# Convert variable vs. hardcode allocations/time

**URL:** <https://discourse.julialang.org/t/convert-variable-vs-hardcode-allocations-time/108211>\
**Category:** General Usage\
**Tags:** performance\
**Created:** [December 31, 2023, 5:10pm UTC](https://discourse.julialang.org/t/convert-variable-vs-hardcode-allocations-time/108211 "2023-12-31T17:10:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattsignorelli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattsignorelli/32/221502_2.png) [@mattsignorelli](https://discourse.julialang.org/u/mattsignorelli)\
**Post date:** [December 31, 2023, 5:10pm UTC](https://discourse.julialang.org/t/convert-variable-vs-hardcode-allocations-time/108211/1 "2023-12-31T17:10:12Z")

</div>

Alright, I’m sure there’s a good answer for this, but I am perplexed.

```julia
julia> using BenchmarkTools: @btime

julia> @btime convert(Float64, 5)
  0.964 ns (0 allocations: 0 bytes)
5.0

julia> a = 5
5

julia> @btime convert(Float64, a)
  102.134 ns (1 allocation: 16 bytes)
5.0

```

What’s happening here? Why does the second `convert` have any allocating and x100 slower? It didn’t help putting the convert into a function, either.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 31, 2023, 5:20pm UTC](https://discourse.julialang.org/t/convert-variable-vs-hardcode-allocations-time/108211/2 "2023-12-31T17:20:11Z")

</div>

See the [Performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/) in the manual. In particular, `a` is an untyped global.

Try `@btime convert(Float64, $a)` or `@btime convert(Float64, a) setup=(a = $a;)` instead.

Also see the BenchmarkTools.jl docs.

---

<div class="post-metadata">

**Author:** ![mattsignorelli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattsignorelli/32/221502_2.png) [@mattsignorelli](https://discourse.julialang.org/u/mattsignorelli)\
**Post date:** [December 31, 2023, 5:34pm UTC](https://discourse.julialang.org/t/convert-variable-vs-hardcode-allocations-time/108211/3 "2023-12-31T17:34:07Z")

</div>

Alright I see now, thank you!
