MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Why so slow and not getting faster

On 20/10/10 09:07, Sam Takoy wrote:
> Hi,
> I ran the following code on two machines (linux and windows) under
> identical versions -
> 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

  • Prev by Date: Re: Table of tables
  • Next by Date: Re: Interpolation undocumented error
  • Previous by thread: Why so slow and not getting faster
  • Next by thread: NDSolve grid