MathGroup Archive 1999

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

Search the Archive

Re: how decrease computation time?

  • To: mathgroup at
  • Subject: [mg18643] Re: how decrease computation time?
  • From: Paul Abbott <paul at>
  • Date: Thu, 15 Jul 1999 01:45:39 -0400
  • Organization: University of Western Australia
  • References: <> <7m1850$5i0$>
  • Sender: owner-wri-mathgroup at

Arnold.Blaisonneau wrote:

> Hi, I' m a new user of Mathematica and I try to translate C program in
> Mathematica language;
> When I do a calculation with Mathematica it takes too many time
> compared with C program (6.4 second versus 0.1second);
> Please excuse me for the lentgh of my message but I really don't know
> what to do for increase the speed;
> (Note that separately each function doesn't take many)
> If any one could help me....
> Thanks

Consider just the first function:

> \!\(\(BessIn[z1_, z2_, n_] :=
>       Module[{i},
>         i = n; \n\t\tz2[0] =
>           Re[BesselI[i, z1[0]\ Exp[I\ z1[1]]]]; \n\t\tz2[1] =
>           Im[BesselI[i, z1[0]\ Exp[I\ z1[1]]]];];\)\n\t\t\n

I see that you have done a very direct translation of the C code. Before
you can call this routine you have to assign (array) values to z1[0] and
z1[1] and you are then writing the output to a second array. Writing
things this way is poor Mathematica style and is likely to slow things

Here is a more Mathematica-like way of writing this function:

BessIn[{z11_, z12_}, n_] := 
  With[{w = BesselI[n, z11*Exp[I*z12]]}, {Re[w], Im[w]}]

If you pass BessIn a list of two values and the parameter n it will
return a list consisting of the real and imaginary parts.  

There are many other things which could be done to speed up your code
and, as others have pointed out, unless you take advantage of
Mathematica's symbolic capabilities, you should expect direct numerical
computation of C-like Mathematica code to be much slower than C.


Paul Abbott                                   Phone: +61-8-9380-2734
Department of Physics                           Fax: +61-8-9380-1014
The University of Western Australia           
Nedlands WA  6907                     mailto:paul at 

            God IS a weakly left-handed dice player

  • Prev by Date: Re: "At long last, Sir, have you no shame?"
  • Next by Date: Re: Need a means to get arguments of a function
  • Previous by thread: Re: how decrease computation time?
  • Next by thread: trouble with printing