MathGroup Archive 2004

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

Search the Archive

Re: Mathematica slows down

  • To: mathgroup at
  • Subject: [mg52987] Re: Mathematica slows down
  • From: Bill Rowe <readnewsciv at>
  • Date: Sat, 18 Dec 2004 04:00:39 -0500 (EST)
  • Sender: owner-wri-mathgroup at

On 12/17/04 at 5:20 AM, george at (George Szpiro)

>the following program runs ok for the first 6,000 iterations, then
>slows down considerably, and then seems to speed up again after
>10,000. Does anyone know what is going on?



>Gap[1]=Prime[k+1]-Prime[k]; Gap[2]=Prime[k+2]-2Prime[k+1]+Prime[k];

>If[Mod[k,1000]==0, Print[ k]]


I don't know why you are seeing the slow down you report, but I do know this computation can be speeded up enormously

First, changing your code slightly so as to retain each of the computed gaps and reducing NumP to make the overall execution time of your code smaller

NumP = 5000; 
gaps = {}; 
  For[k = 1, k < NumP, k++, 
   gaps =
        Prime[k+4]-4*Prime[k+3]+6*Prime[k+2]-4*Prime[k+1]+Prime[k]}]]; ]

{1.2999999999999972*Second, Null}

Now for a different way to do the same computation

Timing[newGaps = 
   Take[#1, NumP-1]&/@Rest[
       ListConvolve[{1, -1},#1]&,Prime/@Range[NumP+3],4]]; ]

{0.020000000000003126*Second, Null}

And finally,

Transpose[newGaps] == gaps


Showing both methods do the same thing.

The improved computation speed results from not computing Prime[k] more than needed and replacing the For loop with Mathematica's functional programming constructs.
To reply via email subtract one hundred and four

  • Prev by Date: Using NMinimize to solve a system of equations
  • Next by Date: Re: Mathematica language issues
  • Previous by thread: Re: Re: Mathematica slows down
  • Next by thread: Re: Re: Mathematica slows down