# Understanding eval

**URL:** <https://discourse.julialang.org/t/understanding-eval/14926>\
**Category:** General Usage\
**Created:** [September 13, 2018, 11:49pm UTC](https://discourse.julialang.org/t/understanding-eval/14926 "2018-09-13T23:49:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rcnlee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rcnlee/32/4107_2.png) [@rcnlee](https://discourse.julialang.org/u/rcnlee)\
**Post date:** [September 13, 2018, 11:49pm UTC](https://discourse.julialang.org/t/understanding-eval/14926/1 "2018-09-13T23:49:14Z")

</div>

I’m trying to understand why there is such a performance difference calling eval vs. just typing it in. The difference doesn’t depend on whether the expression depends on any variables. How can I make the eval version much faster? I need to use eval because this is genetic programming code and the expressions are generated on the fly.

Thanks!

```julia
julia> ex=:(100+cos(4.0)/4.0*sin(1.0)+123.123)
:(100 + (cos(4.0) / 4.0) * sin(1.0) + 123.123)

julia> eval(ex)
222.9854944646596

julia> @btime eval(ex)
  115.154 μs (54 allocations: 3.38 KiB)
222.9854944646596

julia> @btime 100+cos(4.0)/4.0*sin(1.0)+123.123
  17.817 ns (0 allocations: 0 bytes)
222.9854944646596

```

---

<div class="post-metadata">

**Author:** ![rcnlee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rcnlee/32/4107_2.png) [@rcnlee](https://discourse.julialang.org/u/rcnlee)\
**Post date:** [September 14, 2018, 12:27am UTC](https://discourse.julialang.org/t/understanding-eval/14926/2 "2018-09-14T00:27:57Z")

</div>

Seems to be worse in Julia 1.0 than 0.6.

---

<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:** [September 14, 2018, 1:41am UTC](https://discourse.julialang.org/t/understanding-eval/14926/3 "2018-09-14T01:41:25Z")

</div>

In the second case you are benchmarking the function

```julia
f() = 100+cos(4.0)/4.0*sin(1.0)+123.123

```

Julia will parse the source code, lower it, infer it, optimize it in Julia, send it to LLVM for further optimization, native code will be generated. This happens once which is visible the first time it is called

```julia
julia> @time f()
  0.030461 seconds (105.33 k allocations: 5.543 MiB, 28.25% gc time)
222.9854944646596

julia> @time f()
  0.000003 seconds (5 allocations: 176 bytes)
222.9854944646596

```

With your `eval` expression you have to start from scratch every time so clearly this will be slower.

For your genetic programming code, what subset of Julia do you need to support? It might be better to write your own little interpreter instead for that subset instead of throwing the whole compiler chain on every single expression. At least if you are only going to evaluate the expression a few times.

---

<div class="post-metadata">

**Author:** ![rcnlee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rcnlee/32/4107_2.png) [@rcnlee](https://discourse.julialang.org/u/rcnlee)\
**Post date:** [September 14, 2018, 3:39am UTC](https://discourse.julialang.org/t/understanding-eval/14926/4 "2018-09-14T03:39:39Z")

</div>

Thanks for the response Kristoffer. This is for ExprOptimization.jl which performs genetic programming (and other algorithms) over context-free grammars. The grammar is specified by the user, but is known ahead of time, including all the possible symbols and function calls.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [September 14, 2018, 5:30am UTC](https://discourse.julialang.org/t/understanding-eval/14926/5 "2018-09-14T05:30:49Z")

</div>

Would `julia - O 0` potentially speed this up slightly?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 14, 2018, 12:35pm UTC](https://discourse.julialang.org/t/understanding-eval/14926/6 "2018-09-14T12:35:21Z")

</div>

You should create the function with `eval` (i.e. do `f = eval(:(......))`) and call that function (you may need `invokelatest`) if you need to call that function multiple times.

---

<div class="post-metadata">

**Author:** ![rcnlee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rcnlee/32/4107_2.png) [@rcnlee](https://discourse.julialang.org/u/rcnlee)\
**Post date:** [September 14, 2018, 4:15pm UTC](https://discourse.julialang.org/t/understanding-eval/14926/7 "2018-09-14T16:15:19Z")

</div>

Generating functions helps speed up multiple calls to the same function. However, repeatedly generating many functions causes the performance of all function calls to degrade.

See: [Degrading performance when generating many functions · Issue #18446 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/18446)

I haven’t tested this lately. But is there any reason to believe that this issue has changed with Julia 1.0?
