Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo strange efficiency problems in Mathematica
    primarykey
    data
    text
    <p>FIRST PROBLEM</p> <p>I have timed how long it takes to compute the following statements (where V[x] is a time-intensive function call):</p> <pre><code>Alice = Table[V[i],{i,1,300},{1000}]; Bob = Table[Table[V[i],{i,1,300}],{1000}]^tr; Chris_pre = Table[V[i],{i,1,300}]; Chris = Table[Chris_pre,{1000}]^tr; </code></pre> <p>Alice, Bob, and Chris are identical matricies computed 3 slightly different ways. I find that Chris is computed 1000 times faster than Alice and Bob.</p> <p>It is not surprising that Alice is computed 1000 times slower because, naively, the function V must be called 1000 more times than when Chris is computed. But it is very surprising that Bob is so slow, since he is computed <em>identically</em> to Chris except that Chris stores the intermediate step Chris_pre.</p> <p>Why does Bob evaluate so slowly?</p> <hr> <p>SECOND PROBLEM</p> <p>Suppose I want to compile a function in Mathematica of the form</p> <pre><code>f(x)=x+y </code></pre> <p>where "y" is a constant fixed at compile time (but which I prefer not to directly replace in the code with its numerical because I want to be able to easily change it). If y's actual value is y=7.3, and I define</p> <pre><code>f1=Compile[{x},x+y] f2=Compile[{x},x+7.3] </code></pre> <p>then f1 runs 50% slower than f2. How do I make Mathematica replace "y" with "7.3" when f1 is compiled, so that f1 runs as fast as f2?</p> <hr> <p>EDIT:</p> <p>I found an ugly workaround for the second problem:</p> <pre><code>f1=ReleaseHold[Hold[Compile[{x},x+z]]/.{z-&gt;y}] </code></pre> <p>There must be a better way...</p>
    singulars
    1. This table or related slice is empty.
    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.
    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