-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Closed
Labels
collectionsData structures holding multiple items, e.g. setsData structures holding multiple items, e.g. setsperformanceMust go fasterMust go faster
Description
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
endResults in
Implicit:
8.669 μs (156 allocations: 13.42 KiB)
Explicit:
5.737 μs (153 allocations: 9.70 KiB)
Metadata
Metadata
Assignees
Labels
collectionsData structures holding multiple items, e.g. setsData structures holding multiple items, e.g. setsperformanceMust go fasterMust go faster