# Performance when dispatching on type

**URL:** <https://discourse.julialang.org/t/performance-when-dispatching-on-type/79617>\
**Category:** Performance\
**Created:** [April 18, 2022, 2:27am UTC](https://discourse.julialang.org/t/performance-when-dispatching-on-type/79617 "2022-04-18T02:27:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArjunNarayanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arjunnarayanan/32/6475_2.png) [@ArjunNarayanan](https://discourse.julialang.org/u/ArjunNarayanan)\
**Post date:** [April 18, 2022, 2:27am UTC](https://discourse.julialang.org/t/performance-when-dispatching-on-type/79617/1 "2022-04-18T02:27:11Z")

</div>

A colleague recently showed me this example, and I’m curious as to the underlying reason for the performance hit.

```julia
using BenchmarkTools, StaticArrays

f(u, v) = u*v'

struct ConcType end

ctyp = ConcType
cvar = ConcType()

F(::Type{ConcType}, u, v) = f(u, v)
F(::ConcType, u, v) = f(u, v)

a = SVector(1.0,2.0)
b = SVector(1.0,2.0,3.0,4.0)

@btime f($a, $b) # 1.700 ns (0 allocations: 0 bytes)
@btime F($cvar, $a, $b) # 1.700 ns (0 allocations: 0 bytes)
@btime F($ctyp, $a, $b) # 122.707 ns (3 allocations: 160 bytes)

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 18, 2022, 6:17am UTC](https://discourse.julialang.org/t/performance-when-dispatching-on-type/79617/2 "2022-04-18T06:17:50Z")

</div>

Hm, maybe benchmark artifacts?

```julia
using BenchmarkTools, StaticArrays

f(u, v) = u*v'

struct ConcType end

F(::Type{ConcType}, u, v) = f(u, v)
F(::ConcType, u, v) = f(u, v)

@btime f(a, b) setup = (a = SVector(1.0,2.0); b = SVector(1.0,2.0,3.0,4.0))
@btime F(cvar, a, b) setup = (cvar = ConcType(); a = SVector(1.0,2.0); b = SVector(1.0,2.0,3.0,4.0))
@btime F(ctyp, a, b) setup = (ctyp = ConcType; a = SVector(1.0,2.0); b = SVector(1.0,2.0,3.0,4.0))

```

results in

```julia
  1.200 ns (0 allocations: 0 bytes)
  1.200 ns (0 allocations: 0 bytes)
  1.200 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![ArjunNarayanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arjunnarayanan/32/6475_2.png) [@ArjunNarayanan](https://discourse.julialang.org/u/ArjunNarayanan)\
**Post date:** [April 18, 2022, 3:52pm UTC](https://discourse.julialang.org/t/performance-when-dispatching-on-type/79617/3 "2022-04-18T15:52:54Z")

</div>

Interesting. So is it generally recommended to use the setup functionality instead of interpolating global variables?

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 18, 2022, 4:02pm UTC](https://discourse.julialang.org/t/performance-when-dispatching-on-type/79617/4 "2022-04-18T16:02:25Z")

</div>

I believe benchmarks in global scope involving non-constants like `ctyp` and `cvar` are problematic, but I don’t know all the details and use setup at least in critical cases like this one.

---

<div class="post-metadata">

**Author:** ![ArjunNarayanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arjunnarayanan/32/6475_2.png) [@ArjunNarayanan](https://discourse.julialang.org/u/ArjunNarayanan)\
**Post date:** [April 18, 2022, 5:13pm UTC](https://discourse.julialang.org/t/performance-when-dispatching-on-type/79617/6 "2022-04-18T17:13:14Z")

</div>

Okay, thank you!
