# Array of functions and allocation

**URL:** <https://discourse.julialang.org/t/array-of-functions-and-allocation/23110>\
**Category:** General Usage\
**Created:** [April 13, 2019, 2:29pm UTC](https://discourse.julialang.org/t/array-of-functions-and-allocation/23110 "2019-04-13T14:29:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pier/32/7335_2.png) [@Pier](https://discourse.julialang.org/u/Pier)\
**Post date:** [April 13, 2019, 2:29pm UTC](https://discourse.julialang.org/t/array-of-functions-and-allocation/23110/1 "2019-04-13T14:29:21Z")

</div>

I am trying to understand why arrays of functions allocate. See the following example:

```julia
using BenchmarkTools
f1(u,v)=1.0-u-v
fv=Array{Function}(undef,1)
fv[1]=f1
u=0.1
v=0.2
@btime f1($u,$v)
0.001 ns (0 allocations: 0 bytes)
@btime fv[1]($u,$v)
71.178 ns (3 allocations: 48 bytes)

```

Why is the second approach so (comparatively) slow and why does it allocate? Am I not benchmarking correctly?  
I would (naively?) expect that two should be almost identical…

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 13, 2019, 2:38pm UTC](https://discourse.julialang.org/t/array-of-functions-and-allocation/23110/2 "2019-04-13T14:38:43Z")

</div>

Because `Function` is an abstract type. If you use a tuple, performance is back:

```julia
julia> ft = (f1,)
(f1,)

julia> @btime $ft[1]($u,$v)
  0.016 ns (0 allocations: 0 bytes)
0.7

```

because

```julia
julia> typeof(ft)
Tuple{typeof(f1)}

```

(also note that you should benchmark the vector with `@btime $fv[1]($u,$v)`)
