Note that there are some explanatory texts on larger screens.

plurals
  1. POJulia, speeding up eval
    primarykey
    data
    text
    <p>Julia has the very nice feature of having access to its own syntactic tree, which makes it easy to generate new functions programatically, but it's much slower than the normal Julia code.</p> <p>For example:</p> <pre><code>julia&gt; timing = @time for i in [1:100] tan(pi/2*rand()); end elapsed time: 1.513e-5 seconds (896 bytes allocated) julia&gt; timing = @time for i in [1:100] x = pi/2*rand(); eval(:(tan(x))); end elapsed time: 0.0080231 seconds (23296 bytes allocated) julia&gt; timing = @time for i in [1:100] eval(:(tan(pi/2*rand()))); end elapsed time: 0.017245327 seconds (90496 bytes allocated) </code></pre> <p>Is there a way to give to <code>eval</code> the same speed as the normal Julia code?</p> <p>EDIT: I was able to slightly speed up eval using the <code>precompile</code> function, but that still not enough:</p> <pre><code>julia&gt; tmp3 = :(sin(x)) :(sin(x)) julia&gt; timing = @time for i in [1:100000] x = pi/2*rand(); eval(tmp3); end elapsed time: 8.651145772 seconds (13602336 bytes allocated) julia&gt; precompile(tmp3,(Float64,Float64)) julia&gt; timing = @time for i in [1:100000] x = pi/2*rand(); eval(tmp3); end elapsed time: 8.611654016 seconds (13600048 bytes allocated) </code></pre> <p>EDIT2:</p> <p>@Ivarne suggested me to provide details on my project. Well, I would like to use the meta-programming capabilities of Julia to calculate the symbolic derivatives and run them.</p> <p>I wrote a function <code>derivative(ex::Expr,arg::Symbol)</code> that takes and expression and an argument, and returns a new expression that is the derivative of <code>ex</code> with respect to <code>arg</code>. Unfortunately, the resulting <code>Expr</code> takes too long to evaluate.</p> <p>EDIT3: as a conclusion, the performances using <code>@eval</code> instead of <code>eval</code>:</p> <pre><code>julia&gt; timing = @time for i in [1:100000] x = pi/2*rand(); @eval(tmp3); end elapsed time: 0.005821547 seconds (13600048 bytes allocated) </code></pre> <p><code>tmp3</code> is still <code>:(sin(x))</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload