On Mar 8, 2016 11:32 AM, "Yichao Yu" <[email protected]> wrote: > > On Tue, Mar 8, 2016 at 11:17 AM, Jared Crean <[email protected]> wrote: > > Hello, > > I did a micro-brenchmark about using Ints or UInt8s as indices for > > randomly reading an array, and discovered that when using Ints (but not > > UInt8s), there is an unexpected memory allocation. More interestingly, the > > size of the memory allocation does not depend on the size of the array (n in > > the code below: > > > > function sumjc(val_arr, idx_arr) > > # random read sum > > sum = 0.0 > > for i=1:length(idx_arr) > > idx = idx_arr[i] > > sum += val_arr[idx] > > end > > > > return sum > > end > > > > function sumjc2(val_arr) > > # sequential read sum > > sum = 0.0 > > for i=1:length(val_arr) > > sum += val_arr[i] > > end > > > > return sum > > end > > > > n = 200 > > a = round(Int, n*rand(n)) + 1 # Int indices > > b = UInt8[ i for i in a] # UInt8 indices > > vals = rand(n + 1) > > > > # warm up > > sumjc(vals, a) > > sumjc(vals, b) > > sumjc2(vals) > > > > # results > > @time sumjc(vals, a) > > println("Int random read @time printed above") > > @time sumjc(vals, b) > > println("UInt8 random read @time printed above") > > @time sumjc2(vals) > > println("standard sum @time printed above") > > > > The output I get is: > > 4.083 microseconds (156 allocations: 10861 bytes) # ??? > > Functions used by @time itself needs to be compiled and causes memory > allocation.
And also other initialization needed when you call it the first time in global scope > > > Int random read @time printed above > > 2.716 microseconds (5 allocations: 160 bytes) > > UInt8 random read @time printed above > > 1.715 microseconds (5 allocations: 160 bytes) > > standard sum @time printed above > > > > > > Is this a bug that should be reported on Github, or is this expected > > behavior? I am using the 0.4 release version of Julia. > > > > Jared Crean
