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.
In the second case you are benchmarking the function
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
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.
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.
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.
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.