# Performance of value assignment inside functions

**URL:** <https://discourse.julialang.org/t/performance-of-value-assignment-inside-functions/84162>\
**Category:** Performance\
**Tags:** question\
**Created:** [July 13, 2022, 11:24am UTC](https://discourse.julialang.org/t/performance-of-value-assignment-inside-functions/84162 "2022-07-13T11:24:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NicolasW](https://avatars.discourse-cdn.com/v4/letter/n/c67d28/32.png) [@NicolasW](https://discourse.julialang.org/u/NicolasW)\
**Post date:** [July 13, 2022, 11:24am UTC](https://discourse.julialang.org/t/performance-of-value-assignment-inside-functions/84162/1 "2022-07-13T11:24:54Z")

</div>

I am puzzled by the performance of Julia, somehow it matters if I fully specialize the function argument.  
Consider the two functions:

```julia
@noinline @inbounds function update_gen!(y::AbstractVector{T}, x::AbstractVector{T}) where {T<:Number}
	y[1] = x[1]
	return nothing
end

@noinline @inbounds function update_float!(y::Vector{Float64}, x::Vector{Float64})
	y[1] = x[1]
	return nothing
end

```

and called with some input

```julia
t_a = [51.31]
t_b = [12.3]

update_gen!(t_a, t_b)
update_float!(t_a, t_b)

```

No matter how I measure performance (`@time` or `@benchmark` …) `update_float!` is always faster by a factor of ~2, while the output of `@code_llvm` and `@code_native` is as expected exactly the same for both version.  
I would be very grateful for an explanation of this behavior.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [July 13, 2022, 11:36am UTC](https://discourse.julialang.org/t/performance-of-value-assignment-inside-functions/84162/2 "2022-07-13T11:36:57Z")

</div>

This is strange indeed, as I don’t see such a difference.

```julia
julia> @benchmark update_float!($t_a, $t_b)
BenchmarkTools.Trial: 10000 samples with 998 evaluations.
 Range (min … max): 14.715 ns … 30.268 ns ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 15.066 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 15.113 ns ± 0.324 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

           ▂▃▃▅█▇ ▂
  ▄▃▁▄▄▅▅▆▇██████▇▃▁▄▃▁▁▄▄▁▁▃▃▁▃▄▃▄▄▅▅▆▅▆▆▅▆▆▆▇▇▆▇▆▇▇▇▇▇▆▇▇▇▆ █
  14.7 ns Histogram: log(frequency) by time 16.2 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark update_gen!($t_a, $t_b)
BenchmarkTools.Trial: 10000 samples with 998 evaluations.
 Range (min … max): 14.693 ns … 1.314 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 15.066 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 15.233 ns ± 13.017 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

                ▁▂▃▂▃▃▇▇█ ▂
  ▄▁▁▁▁▄▄▃▁▅▄▆▅▆██████████▆▁▃▁▁▃▁▁▁▁▁▁▁▃▁▁▃▄▄▁▃▁▃▃▁▃▁▃▃▁▃▄▄▄▄ █
  14.7 ns Histogram: log(frequency) by time 15.7 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

```

---

<div class="post-metadata">

**Author:** ![NicolasW](https://avatars.discourse-cdn.com/v4/letter/n/c67d28/32.png) [@NicolasW](https://discourse.julialang.org/u/NicolasW)\
**Post date:** [July 13, 2022, 11:49am UTC](https://discourse.julialang.org/t/performance-of-value-assignment-inside-functions/84162/3 "2022-07-13T11:49:44Z")

</div>

Try dropping the interpolation of the in/output and the `Float64`version becomes significantly faster.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/1/81b51c49c1ff3e9c0c468798953535b166f4ce77.png)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 13, 2022, 1:20pm UTC](https://discourse.julialang.org/t/performance-of-value-assignment-inside-functions/84162/4 "2022-07-13T13:20:55Z")

</div>

> [@NicolasW](#):
>
> Try dropping the interpolation of the in/output and the `Float64`version becomes significantly faster.

Dropping the interpolation means that you are timing dynamic dispatch, which is not typically relevant to performance in real applications.
