# Function array performance

**URL:** https://discourse.julialang.org/t/function-array-performance/78889
**Category:** Performance
**Created:** [April 1, 2022, 9:53pm UTC](https://discourse.julialang.org/t/function-array-performance/78889 "2022-04-01T21:53:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![smith-isaac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smith-isaac/32/35124_2.png) [@smith-isaac](https://discourse.julialang.org/u/smith-isaac)
#### Post date: [April 1, 2022, 9:53pm UTC](https://discourse.julialang.org/t/function-array-performance/78889/1 "2022-04-01T21:53:18Z")

</div>

I have an array of functions that each return a float, and so I am trying to assign the results of those functions to an array. So I had something like this

```julia
results = similar(f_arr, Float64)
results .= [f(a, b, c) for f in f_arr]

```

however this is 10+ times slower than manually calling each function in the array, as in

```julia
results = similar(f_arr, Float64)
results[1] = f1(a, b, c)
results[2] = f2(a, b, c)
...

```

Is there an explanation as to why the first way is so slow? I also tried something like the following

```julia
results = similar(f_arr, Float64)
results .= [f_arr[i](a, b, c) for i in 1:length(f_arr)]

```

and I got an error because the tuple `(a, b, c)` was getting passed to `f_arr[i]` instead of passing all three variables in the first two code blocks. I am fairly new to Julia so maybe there is some piece of documentation I am missing, and I would love to be pointed in the right direction.

---

<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: [April 1, 2022, 10:01pm UTC](https://discourse.julialang.org/t/function-array-performance/78889/2 "2022-04-01T22:01:03Z")

</div>

Functions all have their own type so this code isn’t type stable.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [April 1, 2022, 10:08pm UTC](https://discourse.julialang.org/t/function-array-performance/78889/4 "2022-04-01T22:08:48Z")

</div>

You might be able to get some improvement by using a tuple instead.

```julia
using BenchmarkTools 

f_array = [sin,cos,tan]
f_tuple = (sin,cos,tan)

x = 1.0

@btime [f($x) for f in f_array];

@btime [f($x) for f in f_tuple];

```

354.717 ns (11 allocations: 272 bytes)  
189.819 ns (3 allocations: 112 bytes)

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 1, 2022, 11:16pm UTC](https://discourse.julialang.org/t/function-array-performance/78889/5 "2022-04-01T23:16:02Z")

</div>

This turns a tuple into an array, which is not optimal. Try to keep it as a tuple:

```julia
1.7.2> @btime ((f,x)->f(x)).($f, $x)
  29.435 ns (0 allocations: 0 bytes)
(0.8414709848078965, 0.5403023058681398, 1.5574077246549023)

```

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [April 2, 2022, 11:48am UTC](https://discourse.julialang.org/t/function-array-performance/78889/6 "2022-04-02T11:48:28Z")

</div>

Same as yours but a bit more elegant:

```julia
julia> @btime map(f->f($x), $f)
  34.945 ns (0 allocations: 0 bytes)
(0.8414709848078965, 0.5403023058681398, 1.5574077246549023)

```

---

<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: [April 2, 2022, 2:05pm UTC](https://discourse.julialang.org/t/function-array-performance/78889/7 "2022-04-02T14:05:26Z")

</div>

> [@smith-isaac](#):
>
> I have an array of functions that each return a float, and so I am trying to assign the results of those functions to an array.

What is the context for this? Maybe there is a more idiomatic way to represent what you are trying to do.
