MathGroup Archive 1999

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

Search the Archive

RE: speed

  • To: mathgroup at
  • Subject: [mg15637] RE: [mg15593] speed
  • From: "Ersek, Ted R" <ErsekTR at>
  • Date: Sat, 30 Jan 1999 04:28:36 -0500 (EST)
  • Sender: owner-wri-mathgroup at

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

It's not hard to write Mathematica programs that look very much  like
programs in Pascal, Fortran, C, etc.

As with your example programs in these languages often use Do, For,
While. This is known as procedural programming and it's almost never
the best way to do things in Mathematica.  As you point out Mathematica
is notoriously slow with procedural programming.

Writing good programs in Mathematica requires you to take a different
approach.  Unless you are doing extremely intense number crunching it's
probably not hard (once you know how) to write programs with acceptable
speed performance.  If you really need the fastest code possible you
can write C programs and run them from within Mathematica (using
MathLink), but then you can have problems such as machine number
overflow and stuff like that.  You can also do this without MathLink by
having your compiled program write the results to a file, and then read
this file with Mathematica.  The advantage of staying inside
Mathematica is that it's very robust and easy to program (once you know
how).  See some examples below.

Fist I define a function f[x].  Notice I don't have to say (x) is a
float, double, etc.  This function will take anything and this is much
of the reason why it's slower than a similar compiled program. In[1]:=
f[x_]:=2 ArcSin[x]

I can give (f) a symbolic argument.

Here I give (f) an approximate number. In[3]:=

Here I give (f) and exact number and get an exact answer.  If you want
to do this with a compiled program you have to write your own program
very much like Mathematica.

Many other systems you wouldn't give you a good answer for the
following. Yes, this answer is right.
3.141592653589793 - 3.525494348078172*I

Next we evaluate f[3] with 30 digits of precision.  You would have a
very hard time doing this with a compiled program. In[6]:=
3.14159265358979323846264338328 - 

A lot of systems would give you machine overflow when you try this next
one. In[7]:=

How would you do this next calculation without Mathematica? In[8]:=

Moral of the story:
You can drive to the grocery store in a big Peterbuilt rig, but don't
complain about how hard it is to get into a parking space.

Ted Ersek

  • Prev by Date: Re: D vs. Derivative
  • Next by Date: Re: harder parts of list
  • Previous by thread: Re: speed
  • Next by thread: Re: speed