Julia: Use sizehint! when creating Dict from Generator

Created on 17 Jun 2020  ·  4Comments  ·  Source: JuliaLang/julia

Dictionaries can be created from generator expressions, but don't seem to use sizehint!. This could give better performance.

I couldn't confirm this in the source code, so I asked on discourse where a benchmark showed fewer allocations when manually calling sizehint! than when using a generator expression.

using BenchmarkTools

println("Implicit:")
@btime begin
    dict = Dict(string(i) => sind(i) for i = 0:5:360)
end

println("Explicit:")
@btime begin
    dict = Dict{String, Int}()
    sizehint!(dict, 360÷5)
    for i in 0:5:360
        dict[string(i)] = i
    end
end

Results in

Implicit:
  8.669 μs (156 allocations: 13.42 KiB)
Explicit:
  5.737 μs (153 allocations: 9.70 KiB)
collections performance

Most helpful comment

I mean, not really, as this is an issue, which that PR can close! it's always nice for a PR author to close an issue, so I suggest to keep this open, now that it has been created :)

All 4 comments

If you want to compare the effect of the sizehint! shouldn't you just measure your explicit one but without the sizehint! call? Otherwise, other differences in the constructor for dictionaires from generators will pollute the result.

Dup of #35254?

Seems so, yes.

I mean, not really, as this is an issue, which that PR can close! it's always nice for a PR author to close an issue, so I suggest to keep this open, now that it has been created :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

omus picture omus  ·  3Comments

Keno picture Keno  ·  3Comments

iamed2 picture iamed2  ·  3Comments

StefanKarpinski picture StefanKarpinski  ·  3Comments

felixrehren picture felixrehren  ·  3Comments