# Anonymous function as argument cant be optimized

**URL:** <https://discourse.julialang.org/t/anonymous-function-as-argument-cant-be-optimized/11814>\
**Category:** General Usage\
**Tags:** performance\
**Created:** [June 20, 2018, 9:50am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument-cant-be-optimized/11814 "2018-06-20T09:50:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [June 20, 2018, 9:50am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument-cant-be-optimized/11814/1 "2018-06-20T09:50:45Z")

</div>

Hello, I have the following (simplified) code where I wonder why the second algorithm runs slower:

```julia
nn(x, bc) = x > 2 ? x : bc(x)

function algorithm_direct(Λ::AbstractArray{T, N}, bc::Function = x->x, bond_condition = (x, y) -> x == y) where {T, N}
	for i ∈ Λ
		bond_condition(i, nn(i, x->x))
	end
end

function algorithm(Λ::AbstractArray{T, N}, bc::Function = x->x, bond_condition = (x, y) -> x == y) where {T, N}
	for i ∈ Λ
		bond_condition(i, nn(i, bc))
	end
end

Λ = [1,2,3,4,5]

@btime algorithm_direct(Λ, x->x, (x, y) -> x == y )
@btime algorithm(Λ, x->x, (x, y) -> x == y )

```

This gives me:

```julia
  401.069 ns (0 allocations: 0 bytes)
  1.536 μs (0 allocations: 0 bytes)

```

So the question is, why the version with the function passed as parameter is slower than the version with fixed function in the call of `nn`.

This is a running minimal example, the actual code is more difficult but the result is the same.

In the original case `@code_warntype` tells me that `bc` is **not** optimized out. I would like to have it behave in this way.

Thanks…

---

<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:** [June 20, 2018, 9:57am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument-cant-be-optimized/11814/2 "2018-06-20T09:57:06Z")

</div>

Don’t benchmark in global scope

```julia
julia> f1(l) = algorithm_direct(l, x->x, (x, y) -> x == y )
f1 (generic function with 1 method)

julia> @btime f1($Λ)
  4.164 ns (0 allocations: 0 bytes)

julia> f2(l) = algorithm(l, x->x, (x, y) -> x == y )
f2 (generic function with 1 method)

julia> @btime f2($Λ)
  3.863 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [June 20, 2018, 10:02am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument-cant-be-optimized/11814/3 "2018-06-20T10:02:23Z")

</div>

Hello, thanks for this quick reply.

However, I still get different result on julia 0.6.2 with your calls:

```julia
  29.229 ns (0 allocations: 0 bytes)
  841.827 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [June 20, 2018, 10:11am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument-cant-be-optimized/11814/4 "2018-06-20T10:11:26Z")

</div>

~~What happens if you move the creation of x-\>x out of the for loop in algorithm\_direct?~~

---

<div class="post-metadata">

**Author:** ![stakaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stakaz/32/4740_2.png) [@stakaz](https://discourse.julialang.org/u/stakaz)\
**Post date:** [June 20, 2018, 10:13am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument-cant-be-optimized/11814/5 "2018-06-20T10:13:07Z")

</div>

How can I do that when I need it for each call of `nn`?

This version behaves exactly the same way

```julia
function algorithm_direct(Λ::AbstractArray{T, N}, bc::Function = x->x, bond_condition = (x, y) -> x == y) where {T, N}
	f = x->x
	for i ∈ Λ
		bond_condition(i, nn(i, f))
	end
end

```

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [June 20, 2018, 10:23am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument-cant-be-optimized/11814/6 "2018-06-20T10:23:05Z")

</div>

Sry, my mistake, I’ve overlooked the ns/us and thought algorithm\_direct was slower.
