MathGroup Archive 1999

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

Search the Archive

Re: speed

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15652] Re: speed
  • From: Paul Abbott <paul at physics.uwa.edu.au>
  • Date: Sat, 30 Jan 1999 04:28:47 -0500 (EST)
  • Organization: University of Western Australia
  • References: <77utg2$jnk@smc.vnet.net> <78pbv2$d0t@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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
____________________________________________________________________


  • Prev by Date: Re: Vector Analysis
  • Next by Date: Re: question [using result of DSolve]
  • Previous by thread: RE: speed
  • Next by thread: RE: speed