Hello,
When we have a variable number of functions fs = [f_1, f_2, ..., f_3] and we want to apply these functions to only one value of x. I’m struggling with performance problems. See:
julia> f1(x) = x^2
julia> f2(x) = x^2 + x
julia> calc(x::Float64, fs::Vector{Function}) =
Float64[ f(x) for f in fs ];
julia> calc(4.0, [f1, f2])
2-element Array{Float64,1}:
16.0
20.0
The answer is right, but there is a performance issue:
julia> @code_warntype calc(4.0, [f1, f2])
Variables
...
f::Function **RED: HERE**
Body::Array{Float64,1}
4 ┄ %19 = @_6::Tuple{Function,Int64}::Tuple{Function,Int64} **RED: HERE**
│ %22 = (f)(x)::Any **RED: HERE**
...
I think it’s because Function is an abstract type. So I tried use a Tuple
instead of Vector
, but the problem persists. The code:
julia> calc(x::Float64, fs::Tuple{Vararg{Function}}) =
Float64[ f(x) for f in fs ]
julia> @code_warntype calc(4.0, (f1, f2))
f::Union{typeof(f1), typeof(f2)} **RED: HERE**
4 ┄ %19 = @_6::Union{Tuple{typeof(f1),Int64},
Tuple{typeof(f2),Int64}}::Union{Tuple{typeof(f1),Int64},
Tuple{typeof(f2),Int64}} **HERE**
│ %22 = (f)(x)::Any **AND HERE**
How can I solve this problem? Thanks.