# Functions of Functions Performance in a Loop

**URL:** <https://discourse.julialang.org/t/functions-of-functions-performance-in-a-loop/34116>\
**Category:** Performance\
**Created:** [February 3, 2020, 2:22pm UTC](https://discourse.julialang.org/t/functions-of-functions-performance-in-a-loop/34116 "2020-02-03T14:22:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [February 3, 2020, 2:22pm UTC](https://discourse.julialang.org/t/functions-of-functions-performance-in-a-loop/34116/1 "2020-02-03T14:22:08Z")

</div>

For the following two loops, in which a function of a function is called in one, and the computation is more explicit in the other, there is a significant amount of memory allocation in the former. It also takes a performance hit of a factor of about 5:

```julia
function Boltzmann(x,V)
    w = exp(-V(x));
    return w;
end

function test_loop1(n,V)
    avg = 0.
    for j in 1:n
        x = randn();
        L = Boltzmann(x,V);
        b = max(1, L)
        avg +=b/n;
    end
    
    return avg;
end

function test_loop2(n,V)
    avg = 0.
    for j in 1:n
        x = randn();
        L = exp(-V(x));
        b = max(1, L)
        avg +=b/n;
    end
    
    return avg;
end

function U(x)
   return 0.5 * x * x; 
end

```

```julia
Random.seed!(100)
@btime test_loop1(10^4,U)
  1.525 ms (60000 allocations: 937.50 KiB)

```

while

```julia
Random.seed!(100)
@btime test_loop2(10^4,U)
  278.115 μs (0 allocations: 0 bytes)

```

This is a fairly trivial example, and it’s not terrible that I would need to code out `exp(-V(x))`. There are, however, other problems where an intermediate computation is needed that is messier, and it would be nice to be able to encapsulate it in a function without taking such a hit. How should I understand this behavior? How can I improve upon it?

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [February 3, 2020, 3:43pm UTC](https://discourse.julialang.org/t/functions-of-functions-performance-in-a-loop/34116/2 "2020-02-03T15:43:02Z")

</div>

I would try

```julia
function test_loop3(n,V::F) where {F}
    avg = 0.
    for j in 1:n
        x = randn();
        L = Boltzmann(x,V);
        b = max(1, L)
        avg +=b/n;
    end
    
    return avg;
end

```

The compiler cannot always specialize on function arguments. AFAICT the heuristic is if the function is called in the body. With the above trick you force specialization anyway.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 3, 2020, 3:45pm UTC](https://discourse.julialang.org/t/functions-of-functions-performance-in-a-loop/34116/3 "2020-02-03T15:45:42Z")

</div>

See [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1.5-dev/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing-1) for the doc entry about this.
