Thanks, this would probably going fastest, but would actually be a project on itself for me, as I don’t have any experience on this.
I found something else (after playing around with @generated
) here
https://discourse.julialang.org/t/a-method-to-remove-the-use-of-runtime-eval-and-invokelatest-as-well-as-support-closures-in-generated-functions-come-with-an-implemented-proptype/
which is condensed into this package
https://github.com/thautwarm/GeneralizedGenerated.jl
#copy&paste into REPL:
using BenchmarkTools
module M
using GeneralizedGenerated
mutable struct Ef
e::Expr
f1::Function
f2
function Ef()
e = :(a + b)
f1 = (a,b)->a+b
f2 = mk_function( :( (a, b) -> $e ) )
new(e,f1,f2)
end
end
function eval_f1(a_ef::Array{Ef,1})
r=0
for ef in a_ef
r += ef.f1(3,5)
end
return r
end
function eval_f2(a_ef::Array{Ef,1})
r=0
for ef in a_ef
r += ef.f2(3,5)
end
return r
end
end
a_ef=[ M.Ef() for i in 1:1000 ]
Performance of the generated function f2
is as fast as the static version f1
:
julia> @btime M.eval_f1($a_ef)
38.800 μs (937 allocations: 14.64 KiB)
8000
julia> @btime M.eval_f2($a_ef)
39.199 μs (937 allocations: 14.64 KiB)
8000