# Lookup in Dict{Int,Float64} allocates

**URL:** <https://discourse.julialang.org/t/lookup-in-dict-int-float64-allocates/75495>\
**Category:** Performance\
**Tags:** dictionary, memory-allocation\
**Created:** [January 31, 2022, 11:02am UTC](https://discourse.julialang.org/t/lookup-in-dict-int-float64-allocates/75495 "2022-01-31T11:02:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnomotani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnomotani/32/26753_2.png) [@johnomotani](https://discourse.julialang.org/u/johnomotani)\
**Post date:** [January 31, 2022, 11:02am UTC](https://discourse.julialang.org/t/lookup-in-dict-int-float64-allocates/75495/1 "2022-01-31T11:02:24Z")

</div>

I was surprised that the following code reports an allocation when looking up a `Float64` value from a `Dict{Int64,Float64}`

```julia
using BenchmarkTools

n = 1_000_000
d = Dict(i=>rand() for i in 1:n);
@btime d[42]
# 19.346 ns (1 allocation: 16 bytes)

```

even more strangely, if I make a `Dict{Int64,String}` instead (like was done [here](https://discourse.julialang.org/t/how-efficient-is-dictionary-lookup-in-julia/26984)), I get 0 allocations

```julia
using BenchmarkTools
using Random

n = 1_000_000
d = Dict(i=>randstring() for i in 1:n);
@btime d[42]
# 19.346 ns (1 allocation: 16 bytes)

```

It seems bizzare that the allocations would depend on the value type of the Dict!

This is not actually a performance issue in my code, because I only have a small number of Dict lookups. I just stumbled across these allocations while profiling and was curious why they happen at all. Does anyone know why?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 31, 2022, 12:16pm UTC](https://discourse.julialang.org/t/lookup-in-dict-int-float64-allocates/75495/2 "2022-01-31T12:16:41Z")

</div>

Don’t benchmark in global scope like that. See some of the top tips in [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/).

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 31, 2022, 12:27pm UTC](https://discourse.julialang.org/t/lookup-in-dict-int-float64-allocates/75495/3 "2022-01-31T12:27:12Z")

</div>

To elaborate on what Kristoffer said, you can either interpolate `d` from global scope:

```julia
julia> @btime $(d)[42]
  4.651 ns (0 allocations: 0 bytes)
0.852560373628357

```

or create a new local `d` using `BenchmarkTools` _tools_:

```julia
julia> @btime d[42] setup=(d = Dict(i=>rand() for i in 1:1_000_000))
  4.720 ns (0 allocations: 0 bytes)
0.20694337049842937

```
