# How does type annotation affect the performance?

**URL:** <https://discourse.julialang.org/t/how-does-type-annotation-affect-the-performance/102986>\
**Category:** Performance\
**Created:** [August 19, 2023, 6:48pm UTC](https://discourse.julialang.org/t/how-does-type-annotation-affect-the-performance/102986 "2023-08-19T18:48:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reza-Reza](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@Reza-Reza](https://discourse.julialang.org/u/Reza-Reza)\
**Post date:** [August 19, 2023, 6:48pm UTC](https://discourse.julialang.org/t/how-does-type-annotation-affect-the-performance/102986/1 "2023-08-19T18:48:56Z")

</div>

I read that in Julia it’s better to allow more generic type annotation and that type annotation be it done or not does not have an impact on the performance. I think I might have misunderstood what I read. So I did a microbenchmark to see what is going on. I defined four functions in which I annotated the types of the inputs in three and did not annotate the type for the fourth function all doing the same thing. Consider the code below:

```julia
using BenchmarkTools

function test_number(x::Vector{Number})
    return sum(x)
end

function test_real(x::Vector{Real})
        return sum(x)
end
    
function test_float(x::Vector{Float64})
    return sum(x)
end

function test_notype(x)
    return sum(x)
end

x_number = ones(Number,100)
x_real = ones(Real,100)
x_float = ones(Float64, 100);

```

`@btime test_number(x_number);` takes 1.590 μs  
`@btime test_real(x_real);` takes 1.589 μs  
`@btime test_float(x_float);` takes 22.573 ns  
`@btime test_notype(x_number);` takes 1.566 μs  
`@btime test_notype(x_real);` takes 1.548 μs  
`@btime test_notype(x_float);` takes 45.605 ns

1. Why does `test_float` function is fastest among these if type annotation does not impact the performance?  
I had the impression that `test_notype` should have been as fast as `test_float` since I thought Julia itself “specialize” this function for float input, however, while `test_notype` is faster than `test_number` and `test_real`, it is not more performant than `test_float`.
2. Why isn’t `test_notype` as fast as `test_float` for float input?
3. When should I type annotate the function arguments for performance?

---

<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:** [August 19, 2023, 6:55pm UTC](https://discourse.julialang.org/t/how-does-type-annotation-affect-the-performance/102986/2 "2023-08-19T18:55:40Z")

</div>

1. The difference isn’t in the functions but in the data. If you do

```julia
function test_number(x::Vector{<:Number})
        return sum(x)
end

```

and call it with `x_float` you will get the same performance as `test_float`.

1. this is benchmarking error. When benchmarking functions that take less than ~100ns you should interploate the arguments to `@btime`. (i.e. `@btime test_notype($x_float)`). Otherwise you will also be measuring the time to figure out the type of the global variable `x_float`.
2. never.

---

<div class="post-metadata">

**Author:** ![Reza-Reza](https://avatars.discourse-cdn.com/v4/letter/r/dc4da7/32.png) [@Reza-Reza](https://discourse.julialang.org/u/Reza-Reza)\
**Post date:** [August 19, 2023, 7:11pm UTC](https://discourse.julialang.org/t/how-does-type-annotation-affect-the-performance/102986/3 "2023-08-19T19:11:19Z")

</div>

Thanks!  
When I call `test_number` on `x_float` that is `test_number(x_float);` I get a method error:  
`MethodError: no method matching test_number(::Vector{Float64})`

I think the issue is `Vector{Number}` is not the same as `Vector{Float64}`, that is while `Float64` is a number and so a sub-type of `Number`, this is not true for `Vector{Number}` and `Vector{Float64}`.

---

<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:** [August 19, 2023, 7:12pm UTC](https://discourse.julialang.org/t/how-does-type-annotation-affect-the-performance/102986/4 "2023-08-19T19:12:33Z")

</div>

Note the difference between `Vector{<:Number}` and `Vector{Number}`. Types in julia are invariant. `Vector{Float64}<:Vector{<:Number}`

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [August 19, 2023, 8:53pm UTC](https://discourse.julialang.org/t/how-does-type-annotation-affect-the-performance/102986/5 "2023-08-19T20:53:30Z")

</div>

As complement to what has been said here, attention that type annotation is particularly important for performances for struct fields.

---

<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:** [August 19, 2023, 10:21pm UTC](https://discourse.julialang.org/t/how-does-type-annotation-affect-the-performance/102986/6 "2023-08-19T22:21:54Z")

</div>

> [@Reza-Reza](#):
>
> When should I type annotate the function arguments for performance?

> [@Oscar\_Smith](#):
>
> never.

See also the section on [argument-type declarations in the manual](https://docs.julialang.org/en/v1/manual/functions/#Argument-type-declarations).
