Two strange efficiency problems in Mathematica

Posted by Jess Riedel on Stack Overflow See other posts from Stack Overflow or by Jess Riedel
Published on 2010-05-01T02:20:25Z Indexed on 2010/05/01 2:27 UTC
Read the original article Hit count: 286


I have timed how long it takes to compute the following statements (where V[x] is a time-intensive function call):

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;

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.

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 identically to Chris except that Chris stores the intermediate step Chris_pre.

Why does Bob evaluate so slowly?


Suppose I want to compile a function in Mathematica of the form


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


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?

Many thanks!

© Stack Overflow or respective owner

Related posts about mathematica

Related posts about optimization