Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: Mathematica slows down

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

On 12/17/04 at 5:20 AM, george at netvision.net.il (George Szpiro)
wrote:


>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?


>NumP=15000;

>For[k=1,k<NumP,k++,

>Gap[1]=Prime[k+1]-Prime[k]; Gap[2]=Prime[k+2]-2Prime[k+1]+Prime[k];
>Gap[3]=Prime[k+3]-3Prime[k+2]+3Prime[k+1]-Prime[k];
>Gap[4]=Prime[k+4]-4Prime[k+3]+6Prime[k+2]-4Prime[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 = {}; 
Timing[
  For[k = 1, k < NumP, k++, 
   gaps =
     Append[
       gaps, 
       {Prime[k+1]-Prime[k], 
        Prime[k+2]-2*Prime[k+1]+Prime[k], 
        Prime[k+3]-3*Prime[k+2]+3*Prime[k+1]-Prime[k],
        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[
     NestList[
       ListConvolve[{1, -1},#1]&,Prime/@Range[NumP+3],4]]; ]

{0.020000000000003126*Second, Null}

And finally,

Transpose[newGaps] == gaps

True

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