Slow Dict comprehension

bbejanov
Fri, 17 Feb 2023 16:51:53 +0000
General Usage

If I construct a Dict using a comprehension I get orders of magnitude slower speed, compared to starting with an empty Dict and running an equivalent loop to fill it in. I’m surprised, because I expected both ways to be roughly of the same time. What am I missing?

Just to be clear, I’m not looking for how to write the fastest code here, just wondering what’s going on.

Thanks!

julia> @time Dict{Symbol,Any}(Symbol(:a,i) => fill(float(i), 10) for i = 1:500);
  0.122558 seconds (40.33 k allocations: 2.259 MiB, 97.40% compilation time)

julia> @time begin
          p = Dict{Symbol,Any}()
          for i = 1:500
             p[Symbol(:a,i)] = fill(float(i), 10)
          end
       end;
  0.000981 seconds (3.01 k allocations: 226.766 KiB)

julia> versioninfo()
Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU E5-2690 v2 @ 3.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, ivybridge)
Environment:
  JULIA_EDITOR = code

6 posts - 4 participants