Re: speed
- To: mathgroup at smc.vnet.net
- Subject: [mg15688] Re: speed
- From: "Yves Gauvreau" <gauy at videotron.ca>
- Date: Sun, 31 Jan 1999 03:06:10 -0500 (EST)
- References: <77utg2$jnk@smc.vnet.net> <78pbv2$d0t@smc.vnet.net> <78um1s$968@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
If it's just the answer you want then 1.003456^100000 0.3 //Timing
get you there in a blink of an eye
Yves
Paul Abbott wrote in message <78um1s$968 at smc.vnet.net>...
>Margit wrote:
>
>> I have a question concerning the numerical speed of Mathematica:
>> I performed the following calculation
>>
>> x=0.3;Print[x]; Do[x=x*1.003456,{i,1,1000000}];Print[x]
>>
>> with Mathematica, it took about 150 seconds.
>
>Reducing the number of iterations by 100, one sees that Do is slow:
>
>In[1]:= x = 0.3; Do[x = x*1.003456, {i, 1, 100000}] // Timing // First
>Out[1]= 8.66 Second
>
>In[2]:= x
>Out[2]=
> 149
>2.04424 10
>
>Using Nest -- which is the natural way of coding this type of operation
>-- one see that Nest runs over 30 times faster.
>
>In[3]:= Nest[1.003456# &, 0.3, 100000] // Timing Out[3]=
> 149
>{0.27 Second, 2.04424 10 }
>
>> The same calculation is performed by TurboPascal in 0.4 seconds.
>
>But is the answer returned by TurboPascal sensible? Mathematica gets
>
> 1497
> 6.474677709 10
>
>Do you get an overflow with TurboPascal?
>
>> I guess that the difference is caused by the working precision. Does
>> anyone know what is the reason for this long duration and how it can
>> be changed?
>
>Part of the difference is that Nest operations in Mathematica are much
>faster than Do loops. You can pseudo-compile your function (which then
>only uses machine numbers) and get a slight speed increase (whilst the
>result is still a machine number)
>
>In[4]:= g = Compile[{a, x, n}, Nest[Function[y, a y], x, n]]; In[5]:=
>g[1.003456, 0.3, 100000] // Timing Out[5]=
> 149
>{0.22 Second, 2.04424 10 }
>
>Cheers,
> Paul
>
>____________________________________________________________________
>Paul Abbott Phone: +61-8-9380-2734
>Department of Physics Fax: +61-8-9380-1014
>The University of Western Australia Nedlands WA 6907
>mailto:paul at physics.uwa.edu.au AUSTRALIA
>http://www.physics.uwa.edu.au/~paul
>
> God IS a weakly left-handed dice player
>____________________________________________________________________
>