When testing some things in the REPL I tried creating a 100x100 matrix of zeros and found it strange that the built-in zeros was slower than creating such a matrix using a Array comprehension.
julia> @btime zeros(Float64,100,100);
7.369 μs (2 allocations: 78.20 KiB)
julia> @btime Float64[0.0 for i=1:100, j=1:100];
4.342 μs (2 allocations: 78.20 KiB)
Why is this the case?
This is wrong. There’s nothing wrong about @btime zeros(Float64, 100, 100)
. What you suggest will only make it hit Overhead caused by widening · Issue #71 · JuliaCI/BenchmarkTools.jl · GitHub
That said, I get more timing variations for the zeros
than for the comprehension. Neither is consistently faster than the other. Could be memory address related.
In particular, you only need to use $
interpolation on non-constant variables (or expressions that you want to evaluate before benchmarking). Float64
is a constant.
2 Likes