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: [mg52982] Re: [mg52956] Mathematica slows down
  • From: DrBob <drbob at bigfoot.com>
  • Date: Sat, 18 Dec 2004 04:00:17 -0500 (EST)
  • References: <200412171020.FAA16192@smc.vnet.net>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

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

Does it matter? If we know why Prime is faster in some ranges than others, what can we do about it?

Still, if you want to speed things up, it's easy to do so.

Here's timing for a code similar to yours:

NumP=15000;
Timing@For[k=1,k<NumP,k++,
     gaps={Prime[k+1]-Prime[k],
         Prime[k+2]-2Prime[k+1]+Prime[k],
         Prime[k+3]-3Prime[k+2]+3Prime[k+1]-Prime[k],
         Prime[k+4]-4Prime[k+3]+6Prime[k+2]-4Prime[k+1]+Prime[k]};
     If[Mod[k,1000]\[Equal]0,Print[gaps]]]

{8,-2,0,10}
{4,4,4,-26}
{8,14,-34,58}
{18,-2,-8,20}
{8,-4,24,-66}
{10,-2,4,-10}
{6,-2,18,-32}
{18,4,-18,30}
{8,4,14,-52}
{14,2,-16,40}
{14,-4,6,-12}
{12,-10,18,-28}
{6,8,-4,-14}
{14,-2,-6,46}
{19.812 Second,Null}

(Timing for your original code was 19.562 Second.)

Here's a code that's 9 times as fast:

Timing[For[k = 1, k < NumP, k++,
    gaps = {{-1, 1, 0, 0, 0}, {1, -2, 1,
         0, 0}, {-1, 3, -3, 1, 0},
        {1, -4, 6, -4, 1}} .
       Table[Prime[k + i], {i, 0, 4}];
     If[Mod[k, 1000] == 0, Print[gaps]]]]

(printed gaps omitted)
{2.172 Second, Null}

And we can still do a lot better:

NumP = 15000;
Timing[k = 1; primes =
     Table[Prime[k + i], {i, 0, 4}];
    While[k < NumP,
     gaps = {{-1, 1, 0, 0, 0},
         {1, -2, 1, 0, 0}, {-1, 3, -3, 1,
          0}, {1, -4, 6, -4, 1}} . primes;
      If[Mod[k, 1000] == 0, Print[gaps]];
      primes = Rest[Append[primes,
         Prime[k + 5]]]; k++]]

(printed gaps omitted)
{0.172 Second, Null}

That's over 100 times faster than the original. (I only expected to get about 15 times faster.)

Bobby

On Fri, 17 Dec 2004 05:20:43 -0500 (EST), George Szpiro <george at netvision.net.il> wrote:

> Hello,
>
> 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?
>
> Thanks,
> George
>
>
> 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]]
>
>
>   ]
>
>
>
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: Re: Mathematica language issues
  • Next by Date: Re: simply problem (not for me) about axes scale
  • Previous by thread: Re: Mathematica slows down
  • Next by thread: Re: Mathematica slows down