MathGroup Archive 1999

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

Search the Archive

Re: speed

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15729] Re: speed
  • From: "Atul Sharma" <mdsa at musica.mcgill.ca>
  • Date: Fri, 5 Feb 1999 03:42:24 -0500 (EST)
  • Organization: McGill University Computing Centre
  • References: <77utg2$jnk@smc.vnet.net> <78pbv2$d0t@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

This is a good example of why it is necessary to pay attention to the
choice of most efficient approach to solving a given problem (as it
turns out, your example is is virtually identical to one contained in
the Mathematica book, under Compile, which may be more clear than my
note below).

A procedural approach is usually not the most efficient strategy for
Mathematica:

In[66]:=x=0.3;Print[x];
Do[x=x*1.003456,{i,1,100000}]//Timing Print[x]

0.3
Out[66]= {1.48 Second, 2.044235^149)

Using the built in Nest function is slightly more efficient, as is
usually the case
In[68]:= Nest[#*1.003456&,0.3,100000]//Timing Out[68]= (1.039 Second,
2.044235^149})

However, since it is a relatively straightforward algebraic expression,
it stands to benefit significantly from being compiled.

In[69]=.
fc=Compile[{{x,_Real},{n,_Integer}},Module[{t},t=x;Do[t=1.003456*t,{n}];t]];
In[70]:= Timing[fc[0.3,100000]]
Out[70]= {0.11 Second, 2.044235^149})

While this is significantly faster, it is restrictive, in that you have
had to specify the input parameters types. In this example, by limiting
n (iterates) to integer values, the range of applicability is
restricted to +- 2^31. If you exceed this, the function defaults to
regular Mathematica implementation and returns the answer more slowly.
By using Compile [ ], you also lose arbitrary precision calculations,
limiting yourself to machine precision.

I hope this helps.

A. Sharma

--------------------------------------------------------------------------
Experience is a hard teacher because she gives the test first, the
lesson afterward.


Atul Sharma MD, FRCP(C)
Pediatric Nephrologist,
McGill University/Montreal Children's Hospital 2300 Tupper, Montreal,
QC, Canada H3H 1P3

email: mdsa at musica.mcgill.ca



Margit wrote in message <78pbv2$d0t at smc.vnet.net>...
>Hi!
>
>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.
>
>The same calculation is performed by TurboPascal in 0.4 seconds.
>
>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?
>
>Thanks!
>
>Margit
>
>
Margit wrote in message <78pbv2$d0t at smc.vnet.net>...
>Hi!
>
>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.
>
>The same calculation is performed by TurboPascal in 0.4 seconds.
>
>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?
>
>Thanks!
>
>Margit
>
>




  • Prev by Date: How to use DisplayTogether[] properly?
  • Next by Date: RE: Repeated calls to Mathematica process
  • Previous by thread: Re: How to use DisplayTogether[] properly?
  • Next by thread: Q: Why can't I stop TraditionalForm dismissing Times Attributes ?