MathGroup Archive 1999

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

Search the Archive

Re: Where's the Speed?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg19814] Re: [mg19764] Where's the Speed?
  • From: "Richard Bills" <Richard_Bills at email.msn.com>
  • Date: Fri, 17 Sep 1999 01:36:56 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Kevin,

You did not give any details about how you implemented the Crank-Nicholson
algorithm, or what functions that you used, so it is difficult to help you
directly.   However, I hope that the following information is helpful.

I have implemented finite difference laser thermal modeling code in both
Mathematica and C, and I did not experience the drastic speed differential
that you describe.  Based on my experience, optimized release C code is
about 5 to 10 times faster than optimized Mathematica code for data array
applications, although the Mathematica numerical results are more accurate
since it keeps track of the number of significant digits and checks for
indeterminate or out of range values.  A more fair comparison would include
additional code in the C version for handling precision issues and out of
range values.

In order to achieve this reasonable performance from Mathematica, I lumped
the whole program into one compiled function using Compile[ ].  I also
isolated and timed each part of the program, then experimented with
different implementations to improve the speed of the function.  This can be
somewhat laborious, but it still takes orders of magnitude less time to
write the whole application (graphics, user interface, other functions, I/O,
etc.) in Mathematica than in C.  I usually begin development without using
Compile or machine precision, then I evolve the design to a more
computationally efficient version using Compile once I am confident in the
results.

Compile uses a virtual machine interpreter.   It is interesting to note that
Java, which also uses a virtual machine interpreter (JVM), is about 2-3
times slower than optimized release C++ code.  Mathematica's virtual machine
is a much higher level interpreter and performs numerous precision and
result checking, therefore it is not surprising to me that it is roughly 2
to 3 times slower than Java.  Incidentally, the speed of the compiler in
Mathematica Version 4 has been greatly improved, especially in the area of
storage of values into arrays.

Compiling also bypasses arbitrary precision math, which can be 25 times
slower than machine precision math in Mathematica.  For example, compare the
computation times of these (on an ancient computer):

b1 = Table[i^10 + 7, {i, 100000}]; // Timing   (* arbitrary precision math
*)
{98.59 Second, Null}

b1[[100000]]
100000000000000000000000000000000000000000000000007

b2 = Table[N[i]^10. + 7.0, {i, 100000}]; // Timing  (* machine precision
math  *)
{3.19 Second, Null}

b2[[100000]]
1.*10^60

The C code shown below (Borland Builder 3.0) performs the same computation
in 0.7 seconds ( 7 seconds for 10^6  values), and is therefore about 5 times
faster than machine precision code in Mathematica.  However, this C code
does not give the correct result that Mathematica gives.

_________________________________________________________________________
#pragma hdrstop
#include <condefs.h>
#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <math.h>




//--------------------------------------------------------------------------
-
#pragma argsused
int main(int argc, char **argv)
{
    time_t  tstart,tend;

    tstart=time(NULL);

    double *data = new double[1000000];

    for(int i=1; i<=1000000; i++)
    {
        data[i-1]=pow(i,10)+7;
    }

    tend=time(NULL);

    printf("computation time:  %d\n", tend-tstart);
    printf("end result:  %f\n",data[1000000-1]);

    int tin;
    scanf("%d", &tin);




        return 0;
}

____________________________________________________________________

If the computation is replaced with the expression ((i*2.3467) + 7.)/3., the
C version is only 4 times faster than Mathematica, which is not bad, all
things considered.

I think it would be helpful if WRI published a book or technical notes on
computational optimization in Mathematica.  They are in the best position to
do so.

Hope that helps.


Sincerely,

Richard E. Bills
Tucson, AZ, USA




-----Original Message-----
From: Kevin J. McCann <kevin.mccann at jhuapl.edu>
To: mathgroup at smc.vnet.net
Subject: [mg19814] [mg19764] Where's the Speed?


>I have read and heard a lot of hype about Mathematica 4.0
>
>"featuring a
>     New Generation of
>                    Fast Numerics"
>
>e.g. on www.wri.com .
>
>However, in the real world it is hard to find anything to get excited
about.
>I have had several disappointing results from 4.0, here are the latest:
>
>On a P450/NT I ran a very simple Crank-Nicholson integration of a
>one-dimensional quantum free-particle wave-packet with x-dimensioned to
1001
>points and time to 401 points -  no error checks or adaptive stepsize, just
>plug-and-chug.  Runtime under 4.0 was 501 seconds, 3.0 was 480 seconds, but
>who's quibbling; however, in FORTRAN this ran in 8 seconds.  I don't even
>consider this to be "fast numerics", but 501 seconds sure isn't. I would
>like to know where all this speed is so I can use some of it, or am I
>missing something?
>
>When I ran the FORTRAN code and then read it into Mathematica, it still was
faster
>than the Mathematica code alone, although the ReadList I used on the ASCII
>file did take some time.  When I upped the dimensions to 2001 x 1001, the
>FORTRAN ran in about 40 s - most of this is for the output; however, when I
>tried to read it in to Mathematica, it took forever, and on the subsequent
plot it
>bombed the Kernel.  I then made the mistake of saving the NB which managed
>to acquire an error so that I can't open it anymore - 6 Mb of useless NB!
>
>
>
>--
>
>Kevin J. McCann
>Johns Hopkins University APL
>
>
>
>
>





  • Prev by Date: Re: Sound and Mathematica
  • Next by Date: Family of integral curves
  • Previous by thread: Re: Where's the Speed?
  • Next by thread: Re: Where's the Speed?