While trying to test my understanding of “views vs slice of arrays” and “row-major vs column major”, I time (using @time) the following functions :
A = rand(10000,10000); function sum_1(x) counter = 0.0 for i in axes(x,2) counter += sum(x[:,i]) end return counter end function sum_2(x) counter = 0.0 for i in axes(x,2) counter += sum(view(x,:,i)) end return counter end function sum_3(x) counter = 0.0 for i in axes(A,2) counter += sum(view(x,i,:)) end return counter end function sum_4(x) counter = 0.0 for I in CartesianIndices(x) counter += x[I] end end function sum_5(x) counter = 0.0 for i in x counter += i end return counter end function sum_6(x) return sum(x) end
I was surprised to consistently observe the following time and memory allocation:
0.485504 seconds (88.47 k allocations: 764.900 MiB, 18.79% gc time)
0.044661 seconds (78.47 k allocations: 1.655 MiB)
0.015908 seconds (68.98 k allocations: 1.510 MiB)
0.110523 seconds (4 allocations: 160 bytes)
0.117049 seconds (5 allocations: 176 bytes)
0.043822 seconds (5 allocations: 176 bytes)
This is not what I would have predicted. I would have predicted the column-major view to be faster than the row-major view, and in both cases I wouldn’t have predicted such high memory allocation.
Can anyone shed some light on the above?