## How Do I Make My Mathematica Programs Run Faster?

New versions of Mathematica often include major speed gains and new built-in functions that can speed up your calculations significantly.

For example, compare Version 4 with Version 3 for calculating autocorrelations.

In Version 3, this calculation takes 150 seconds.

Version 4 is about five times faster right out of the box.

But Version 4 also has a new built-in function for calculating list correlations. Using this, the calculation is over 340 times faster than before.

Some other speed improvements in Mathematica 4 include:

Use Built-in Functions

Built-in Mathematica functions are usually far more efficient and faster than user-written code with the same functionality. Part of the reason for this difference is that many of them use proprietary algorithms, are highly optimized, and are implemented in C code. Looking to see if a function you want to implement is already built into Mathematica is time well spent. Also keep in mind that Mathematica contains not only thousands of mathematical functions but also a large number of utility functions such as Sort.

For example, here is a pretty nifty way to calculate an autocorrelation.

The above is about the most terse top-level code possible for this calculation. At least it's a lot better than the following:

And it looks reasonably fast for small sets of numbers.

But let's try this on a rather large set of data.

The built-in function ListCorrelate cuts the time needed for the large data set by a factor of over 2350.

Use Machine-Precision Numbers

One of the most important things to notice when doing numerical calculations is that Mathematica uses fundamentally different evaluation mechanisms for floating-point numbers than for exact numerical quantities like integers and fractions. Usually, the floating-point calculations are much faster than exact arithmetic, so you should use them wherever appropriate.

Take a look at the following example.

Mathematica takes about 2.36 seconds to calculate the eigenvalues of this matrix. Actually, this is pretty fast. But when you simply change the entries to floating points,

the calculation of the eigenvalues is all but instantaneous.

Avoid Procedural Programming

For users coming from other languages like C, Fortran, or Matlab, using procedural constructs is tempting. But procedural programming compared to functional programming gives, with very few exceptions, extremely poor performance.

Operate on Lists as a Whole, Not on Individual Parts

Mathematica is able to operate on whole lists at once or, using the new PackedArray technology, even on lists in an internal compressed form. This method is much faster than stepping through lists and calculating single elements. Also, keep in mind list-manipulation functions such as Partition.

The speed increase is already quite significant even for simple operations such as taking the sine of all elements in a list.

For a more realistic example, let's say we have two vectors of x and y values that we want to combine into a list of {x, y} pairs.

Constructing a new table would be a pretty straightforward approach.

Combining, transposing, and partitioning the lists gives the same result but is a great deal faster.

Use Compile

In many numerical computations, one of the easiest ways to get better performance is to use the built-in compiler. Often, doing this is as easy as wrapping Compile[args, ...] around your function.

Use the Parallel Computing Toolkit

If you have to run large-scale calculations, the Parallel Computing Toolkit might be just the solution you are looking for.

The Parallel Computing Toolkit brings parallel computation to anybody having access to more then one computer on a network or to a multiprocessor computer. It implements many parallel-programming primitives and includes high-level commands for parallel execution of operations such as animation, plotting, and matrix manipulation. Also supported are many popular new programming approaches such as parallel Monte Carlo simulation, visualization, searching, and optimization. The implementations for all high-level commands in the Parallel Computing Toolkit are provided in Mathematica source form, and they can serve as templates for building additional parallel programs.

Use MathCode C++

MathCode C++ is an application package for Mathematica that generates optimized C++ code for numerical computations.

Some of Its Features and Advantages
• Compiled code can easily be called from within Mathematica.
• Optionally, stand-alone code can be generated (i.e., code that can be executed independently of Mathematica).
• Performance increases by up to five thousand times for some computations.
• External libraries of C, C++, and Fortran77 code can easily be connected and called from within Mathematica.
• Extended functionality is available for extracting parts of matrices.