MathGroup Archive 2012

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

Search the Archive

Re: How I can I optimize the following code in order to get very

On Tuesday, 21 February 2012 13:10:37 UTC+2, Liwen Zhang  wrote:
> How I can I optimize the following code in order to get very short
> running time?
> Time1 = AbsoluteTime[];
> n = 3000;
> Do[Do[x =
>      Sin (45.0)*ArcSin (0.5)*Sqrt (5.0)*ArcTan (2.5555);, {n}], {n}];
> Time2 = AbsoluteTime[];
> Print[Time2 - Time1];

This program does not do anything except calculate the same thing n*n times.  Since you have no varying parameters in the expression, you can just calculate the value once.

Generally, a good and easy way to speed up programs is compiling them:

Here's a detailed list of tips and guidelines on how to use Compile effectively, by Leonid Shifrin:

Here's another post on general performance tuning in Mathematica, also written by Leonid Shifrin:

Here's a more advanced tutorial on using Compile[], from the docs:

Finally, you'll find a collection of link to performance tuning tutorials / presentations here:

as well as several discussions on performance tuning here:

  • Prev by Date: Re: How best to implement a hash table in Mathematica
  • Next by Date: Re: good list
  • Previous by thread: Retrieving numerical value for ImagePadding
  • Next by thread: Re: Kolmogorov Smirnov in two or more dimensions is in Mathematica 8.0.4