Re: Re: Mathematica slows down

• To: mathgroup at smc.vnet.net
• Subject: [mg53000] Re: [mg52964] Re: [mg52956] Mathematica slows down
• From: DrBob <drbob at bigfoot.com>
• Date: Sun, 19 Dec 2004 06:14:46 -0500 (EST)
• References: <200412180859.DAA02187@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Bob, I'm puzzled.

NumP = 15000;
Timing[
Table[pk[n] = Prime[n], {n, NumP}];
For[k = 1, k â?¤ NumP, k++, Gap[1] = pk[k + 1] - pk[k];
Gap[2] = pk[k + 2] - 2pk[k + 1] + pk[k];
Gap[3] = pk[k + 3] - 3pk[k + 2] + 3pk[k + 1] - pk[k];
Gap[4] = pk[k + 4] - 4pk[k + 3] + 6pk[k + 2] - 4pk[k + 1] + pk[k];
If[Mod[k, 1000] == 0, Print[k]]]
]

(prints omitted)
{0.671 Second, Null}

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++]]

(prints omitted)
{0.156 Second, Null}

I'm puzzled that we get these huge speed-ups from eliminating redundant calls to Prime. The original code called it 14 times for each k, while these two solutions call Prime only once for each -- without significantly reducing other operation counts, or so it appears. Yet your code is 30 times faster and mine is 100 times faster (with the same number of calls to Prime). I'd expect us to get at most a 14-to-one speed increase, or thereabouts.

Bobby

On Sat, 18 Dec 2004 03:59:37 -0500 (EST), Bob Hanlon <hanlonr at cox.net> wrote:

> I don't know what changes; however, you can speed it up by avoiding
> repeated calls of Prime for the same values
>
> NumP=15000;
>
> Table[pk[n]=Prime[n],{n,NumP}];
>
> For[k=1,k<NumP,k++,
>   Gap[1]=pk[k+1]-pk[k];
>   Gap[2]=pk[k+2]-2pk[k+1]+pk[k];
>   Gap[3]=pk[k+3]-3pk[k+2]+3pk[k+1]-pk[k];
>   Gap[4]=pk[k+4]-4pk[k+3]+6pk[k+2]-4pk[k+1]+pk[k];
>   If[Mod[k,1000]==0,Print[k]]]
>
>
> Bob Hanlon
>
>>
>> From: George Szpiro <george at netvision.net.il>
To: mathgroup at smc.vnet.net
> To: mathgroup at smc.vnet.net
>> Date: 2004/12/17 Fri AM 05:20:43 EST
>> To: mathgroup at smc.vnet.net
>> Subject: [mg53000] [mg52964] [mg52956] Mathematica slows down
>>
>> 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: creating exact numbers, strange behavior
• Next by Date: Re: Vector Piecewise in 5.1
• Previous by thread: Re: Mathematica slows down
• Next by thread: Re: Mathematica slows down