# \`IdDict{UInt64, Float64}\` is much slower than \`Dict{UInt64, Float64}\` for retrieving values

**URL:** https://discourse.julialang.org/t/iddict-uint64-float64-is-much-slower-than-dict-uint64-float64-for-retrieving-values/123144
**Category:** Performance
**Tags:** dictionary
**Created:** [November 27, 2024, 4:01am UTC](https://discourse.julialang.org/t/iddict-uint64-float64-is-much-slower-than-dict-uint64-float64-for-retrieving-values/123144 "2024-11-27T04:01:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)
#### Post date: [November 27, 2024, 4:01am UTC](https://discourse.julialang.org/t/iddict-uint64-float64-is-much-slower-than-dict-uint64-float64-for-retrieving-values/123144/1 "2024-11-27T04:01:41Z")

</div>

MWE:

```julia
julia> k = rand(UInt, 5000);

julia> v = rand(Float64, 5000);

julia> using Random

julia> ks = shuffle(k);

julia> d1 = Dict(k .=> v);

julia> d2 = IdDict(k .=> v);

julia> using BenchmarkTools

julia> @benchmark begin
           for i in $ks
               ($d1)[i]
           end
       end
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 11.500 μs … 67.900 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 11.700 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 11.786 μs ± 1.129 μs ┊ GC (mean ± σ): 0.00% ± 0.00%

  ▃██ ▆▂ ▁ ▁ ▂
  ███▁██▁▆█▁█▅▇▁█▅▁██▁▄▁▁▅▅▄▁▅▄▁▄▄▁▃▄▅▁▅▄▁▄▅▁▄▄▁▄▄▁▁▄▄▁▄▃▁▃▁▃ █
  11.5 μs Histogram: log(frequency) by time 15.6 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark begin
           for i in $ks
               ($d2)[i]
           end
       end
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 60.800 μs … 424.700 μs ┊ GC (min … max): 0.00% … 79.09%
 Time (median): 62.500 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 65.249 μs ± 16.455 μs ┊ GC (mean ± σ): 1.88% ± 5.92%

  ▃█▅▃▂ ▁▁ ▁
  ███████▇▇▇▇▇▇▇▇▇████▇▇▆▅▆▆▆▅▅▅▅▄▅▆▃▅▄▄▅▅▄▅▄▃▅▄▄▅▄▅▄▅▁▄▃▁▅▅▄▃ █
  60.8 μs Histogram: log(frequency) by time 118 μs <

 Memory estimate: 78.12 KiB, allocs estimate: 5000.

```

Is this expected? I thought `IdDict` would be at least as performant as `Dict` (even) for bits types. Thanks!

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [November 27, 2024, 4:06am UTC](https://discourse.julialang.org/t/iddict-uint64-float64-is-much-slower-than-dict-uint64-float64-for-retrieving-values/123144/2 "2024-11-27T04:06:07Z")

</div>

The `IdDict` is implemented in C and therefore doesn’t get quite as well optimized as a regular `Dict`.

---

<div class="post-metadata">

### Author: ![frankwswang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frankwswang/32/18561_2.png) [@frankwswang](https://discourse.julialang.org/u/frankwswang)
#### Post date: [November 27, 2024, 8:36pm UTC](https://discourse.julialang.org/t/iddict-uint64-float64-is-much-slower-than-dict-uint64-float64-for-retrieving-values/123144/3 "2024-11-27T20:36:16Z")

</div>

Cool. Thanks!
