Expression timing; a black art?

*To*: mathgroup at smc.vnet.net*Subject*: [mg63234] Expression timing; a black art?*From*: AES <siegman at stanford.edu>*Date*: Tue, 20 Dec 2005 04:19:30 -0500 (EST)*Organization*: Stanford University*Sender*: owner-wri-mathgroup at wolfram.com

OK, to follow up on my own recent post, I did some timing tests for the function I asked about earlier, as a function or as a Module[] with a repeated square root pulled out. To say the results are puzzling (to me, anyway) is putting it mildly. Approach: Create a notebook with three sections, each of the form: x; Remove["Global`*"]; fn := (function as below) a=0.12; xmax=3.5; dx=0.1; tn=Timing[Table[{x, fn[a,x]//N}, {x,0,xmax,dx}]]; where the three functions "fn" are: f1[a_,x_] := Sum[Exp[-(Pi a n)^2- ((x-n Sqrt[1-(Pi a^2)^2])/a)^2], {n,-Infinity,Infinity}]; f2[a_,x_] := Module[{b}, b=Sqrt[1-(Pi a^2)^2]; Sum[Exp[-(Pi a n)^2-((x-n b)/a)^2], {n,-Infinity,Infinity}]]; f3[a_,x_] := Module[{b,c1,c2}, b=Sqrt[1-(Pi a^2)^2]; c1=(Pi a)^2; c2=1/a^2; Sum[Exp[-c1 n^2-c2(x-n b)^2], {n,-Infinity,Infinity}]]; Summary of results to date: 1) Open Mathematica, run notebook first time with functions in f1, f2, f3 order. Timings are (in round numbers, +/-10%) t1 = 30 sec, t2 = 5 sec, t3 = 20 sec. 2) Re-run same notebook from top: Timings are now 5 sec, 5 sec, 5 sec. Clearly Mathematica is remembering *something* from the first run, despite the Remove[Global] in each section . . . ? 3) Quit Mathematica, re-Open, reorder sections in f2, f1, f3 order. Timings on first run are now t2 = 30 sec, t1 = 5 sec, t3 = 20 sec; timings on second run are again 5, 5, 5 sec. 4) Quit Mathematica, re-Open, reorder sections in f3, f2, f1 order. Timings on first run are 30, 20, 5 sec. Conclusion #1: Running *either* f1 or f2 once leaves something (?) in the kernel that greatly speeds up the f2 or f1, and gives a little help to f3. Running f3 first gives a little help to f2 (30 down to 20), and probably also to f1 (didn't try), but doesn't push it all the way down to 5. Conclusion #2: Using modular form with Sqrt[] pulled out doesn't help at all. Conclusion #3: If a naive user like me had only done the very first test above, I'd have been left believing that pulling the Sqrt[] out *did* help. Conclusion #4: Trying to understand Mathematica timing is a very black art. Hypothesis: Running any of these functions on a *random* set of values the first time, then another random set the second time, will *not* speed up the second run for either the same fn or any of the other ones. Anyone want to predict if this is so?

**Follow-Ups**:**Re: Expression timing; a black art?***From:*"Carl K. Woll" <carlw@wolfram.com>

**Re: Expression timing; a black art?***From:*ggroup@sarj.ca