       RE: speed

• To: mathgroup at smc.vnet.net
• Subject: [mg15637] RE: [mg15593] speed
• From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
• Date: Sat, 30 Jan 1999 04:28:36 -0500 (EST)
• 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. 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:=
f[x_]:=2 ArcSin[x]

I can give (f) a symbolic argument.
In:=
f[a]
Out=
2*ArcSin[a]

Here I give (f) an approximate number. In:=
f[0.2]
Out=
0.4027158415806615

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.
In:=
f[1/2]
Out=
Pi/3

Many other systems you wouldn't give you a good answer for the
following. Yes, this answer is right.
In:=
f[3.0]
Out=
3.141592653589793 - 3.525494348078172*I

Next we evaluate f with 30 digits of precision.  You would have a
very hard time doing this with a compiled program. In:=
N[f,30]
Out=
3.14159265358979323846264338328 -
3.5254943480781721009304372999*I

A lot of systems would give you machine overflow when you try this next
one. In:=
Exp[1800.0]/1800.0
Out=
2.98397321695*^778

How would you do this next calculation without Mathematica? In:=
BernoulliB
Out=
-24655088825935372707687196040585199904365267828865801/30

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.

Cheers,
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