Re: Why so slow and not getting faster

*To*: mathgroup at smc.vnet.net*Subject*: [mg113261] Re: Why so slow and not getting faster*From*: David Bailey <dave at removedbailey.co.uk>*Date*: Thu, 21 Oct 2010 07:01:31 -0400 (EDT)*References*: <i9m7vi$j5c$1@smc.vnet.net>

On 20/10/10 09:07, Sam Takoy wrote: > Hi, > > I ran the following code on two machines (linux and windows) under > identical versions - 7.0.1.0. > > On the windows machine, the code takes 10-15 seconds on each run. > > On the linux maxhine, the first run took 6 seconds and all subsequent > runs are nearly instantaneous (< 1/10 sec). > > 1. I accept that my windows machine is slower, but why doesn't it show > similar acceleration in subsequent runs? > > 2. Even 6 seconds I think is too high. Are these very simple > integrations for Mathematica? > > Many thanks in advance, > > Sam > > > FourierCoefficients[f_, order_] := Module[{first, reim}, > first = 1/(2 \[Pi]) Integrate[f[a], {a, -\[Pi], \[Pi]}]; > Print[first]; > reim = Table[(Print[n]; > 1/\[Pi] Integrate[f[a]*E^(I*n*a), {a, -\[Pi], \[Pi]}]), {n, 1, > order}]; > Join[{first}, Map[ComplexExpand[{Re[#], Im[#]}]&, reim]]] > > g[a_] := 1/32 (7 - 11 Cos[2 a] - 8 Cos[4 a] - 3 Cos[6 a]) > FourierCoefficients[g, 5] > Integrate (as opposed to NIntegrate) is a complicated symbolic operation, and you can't expect lightening speed. A lot of checks are performed to avoid returning invalid results of various sorts. Integrate will sometimes store results for re-use (a process known as caching) and this is almost certainly responsible for the speed up when you repeat the calculation. The exact decision as to when to keep a result, and when to discard it is obviously somewhat arbitrary, and Mathematica may store more results if more memory is available - which could account for the difference between your two machines. David Bailey http://www.dbaileyconsultancy.co.uk