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

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 )

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

This version behaves exactly the same way

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