# Anonymous functions slower than proper functions?

**URL:** <https://discourse.julialang.org/t/anonymous-functions-slower-than-proper-functions/73275>\
**Category:** Performance\
**Tags:** differentialequation\
**Created:** [December 17, 2021, 5:29pm UTC](https://discourse.julialang.org/t/anonymous-functions-slower-than-proper-functions/73275 "2021-12-17T17:29:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FHell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fhell/32/2421_2.png) [@FHell](https://discourse.julialang.org/u/FHell)\
**Post date:** [December 17, 2021, 5:29pm UTC](https://discourse.julialang.org/t/anonymous-functions-slower-than-proper-functions/73275/1 "2021-12-17T17:29:19Z")

</div>

Is this expected behavior? I see a performance difference of a factor two for very simple functions. E.g.:

```julia
using BenchmarkTools

g1 = t -> cos(t - 0.5 * t^2)
g2(t) = cos(t - 0.5 * t^2)

function h1()
    x = 0.
    for i in 1.:10000.
        x += g1(i)
    end
    x
end

function h2()
    x = 0.
    for i in 1.:10000.
        x += g2(i)
    end
    x
end

b3 = @benchmark h1()
b4 = @benchmark h2()

printstyled("Anonymous function:\n\n"; bold=true)
display(b3) # min 745.989 μs

printstyled("One line function:\n\n"; bold=true)
display(b4) # min 402.577 μs

```

Came up because DiffEq has anonymous functions in the Documentation here:

[https://diffeq.sciml.ai/stable/tutorials/ode\_example/#Example-3:-Solving-Nonhomogeneous-Equations-using-Parameterized-Functions](https://diffeq.sciml.ai/stable/tutorials/ode_example/#Example-3:-Solving-Nonhomogeneous-Equations-using-Parameterized-Functions)

At a very quick glance I didn’t see any warnings about anonymous functions and performance anywhere either.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 17, 2021, 5:36pm UTC](https://discourse.julialang.org/t/anonymous-functions-slower-than-proper-functions/73275/2 "2021-12-17T17:36:36Z")

</div>

The problem is that `g1` is a non-constant global, if you use

```julia
const g1 = t -> cos(t - 0.5 * t^2)

```

the performance should be the same. (or if you put everything inside a function, making `g1` local to the function scope).

---

<div class="post-metadata">

**Author:** ![FHell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fhell/32/2421_2.png) [@FHell](https://discourse.julialang.org/u/FHell)\
**Post date:** [December 17, 2021, 7:18pm UTC](https://discourse.julialang.org/t/anonymous-functions-slower-than-proper-functions/73275/3 "2021-12-17T19:18:29Z")

</div>

Thank you. I completely missed that. And this was on top of a refactor where I removed globals as well.

So this doesn’t actually make a difference if I pass the global to another function, e.g. a solver from DiffEq:

```julia

using BenchmarkTools

g1 = t -> cos(t - 0.5 * t^2)
g2(t) = cos(t - 0.5 * t^2)

function h(g)
    x = 0.
    for i in 1.:10000.
        x += g(i)
    end
    x
end

b3 = @benchmark h($g1)
b4 = @benchmark h($g2)

printstyled("Anonymous function:\n\n"; bold=true)
display(b3) # min 402.685 μs

printstyled("One line function:\n\n"; bold=true)
display(b4) # min 402.083 μs

```
