# Best performance for eval of expressions with parameters

**URL:** <https://discourse.julialang.org/t/best-performance-for-eval-of-expressions-with-parameters/43443>\
**Category:** Performance\
**Tags:** eval\
**Created:** [July 21, 2020, 6:52pm UTC](https://discourse.julialang.org/t/best-performance-for-eval-of-expressions-with-parameters/43443 "2020-07-21T18:52:55Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 28, 2020, 2:07pm UTC](https://discourse.julialang.org/t/best-performance-for-eval-of-expressions-with-parameters/43443/3 "2020-07-28T14:07:40Z")

</div>

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/](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](https://github.com/thautwarm/GeneralizedGenerated.jl)

```julia
#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
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

```

---

_[View the full topic](https://discourse.julialang.org/t/best-performance-for-eval-of-expressions-with-parameters/43443)._
