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[1]:= f[x_]:=2 ArcSin[x] I can give (f) a symbolic argument. In[2]:= f[a] Out[2]= 2*ArcSin[a] Here I give (f) an approximate number. In[3]:= f[0.2] Out[3]= 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[4]:= f[1/2] Out[4]= Pi/3 Many other systems you wouldn't give you a good answer for the following. Yes, this answer is right. In[5]:= f[3.0] Out[5]= 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]:= N[f[3],30] Out[6]= 3.14159265358979323846264338328 - 3.5254943480781721009304372999*I A lot of systems would give you machine overflow when you try this next one. In[7]:= Exp[1800.0]/1800.0 Out[7]= 2.98397321695*^778 How would you do this next calculation without Mathematica? In[8]:= BernoulliB[76] Out[8]= -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