Benchmarking questions

Dear all, I was testing some acceleration of for loops (and reducing the allocations), and I made a less complex example for testing:

Julia>julia> function test_1(a,b)
           @views for i in 1:a, j in 1:b
               c = i + j
           end
       end
test_1 (generic function with 1 method)
julia> @benchmark test_1(5,5)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max):  1.500 ns … 8.291 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     1.583 ns             ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.566 ns ± 0.084 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

                     ▇                 █▁
  ▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂ ▂
  1.5 ns         Histogram: frequency by time       1.62 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> function test_2(a,b)
           for i in 1:a, j in 1:b
               c = i + j
           end
       end
test_2 (generic function with 1 method)

julia> @benchmark test_2(5,5)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max):  1.500 ns … 6.792 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     1.583 ns             ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.566 ns ± 0.076 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

                     ▇                 █▁
  ▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂ ▂
  1.5 ns         Histogram: frequency by time       1.62 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

Shouldn’t I expect the function test_1 to be faster due to the usage of @views?

@views only makes things faster when you are actually allocating any slices. There aren’t any here.

Just to expand on what’s above, note that scalars don’t allocate.

So if you access a variable holding a number or a single element of a vector (for example, x=[1,2,3] and you access x[1]), then you don’t need views.

You’d need a view if you have a slice with two or more elements, such as x[1:2] .

Note also that your benchmarks are incorrect since Julia optimizes your loops away because your functions don’t return anything, and so independently by the magnitude of a and b it takes a small amount of ns:


julia> @benchmark test_1(1000000000,10000000000)
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min … max):  4.700 ns … 73.700 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     5.700 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   5.514 ns ±  1.185 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▅ █       ▁ ▂  ▂ ▃  ▃ ▄  ▆ █  ▇ ▅  ▄ ▃  ▁                  ▂
  █▁█▁▁▅▁█▁▁█▁█▁▁█▁█▁▁█▁█▁▁█▁█▁▁█▁█▁▁█▁█▁▁█▁█▁▁▇▁▇▁▁▆▁▇▁▁█▁█ █
  4.7 ns       Histogram: log(frequency) by time        7 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.
1 Like