182
$\begingroup$

What performance tuning tricks do you use to make a Mathematica application faster? MATLAB has an amazing profiler, but from what I can tell, Mathematica has no similar functionality.

$\endgroup$
4
  • 3
    $\begingroup$ What do I do if I want to accept two answers??? Never had that happen before. Thanks for the awesome responses! $\endgroup$
    – John
    Commented Jan 20, 2011 at 4:56
  • 2
    $\begingroup$ Jon McLoone's article entitled 10 Tips for writing fast Mathematica code $\endgroup$
    – tlehman
    Commented Jan 5, 2012 at 3:54
  • 3
    $\begingroup$ @Tobi While most of Jon's suggestions are very valid and many overlap with the suggestions in the answers below, his suggestion to use Block in place of Module can be a dangerous practice that I'd rather avoid (except possibly inside packages, but may be even there). For a minimal efficieny boost, you get a danger of variable conflicts which can be a debugging nightmare in large projects. The situation is somewhat better when you use Block inside a package, since then the name conflicts can only happen with symbols from the same package (context). $\endgroup$ Commented Jan 5, 2012 at 15:37
  • $\begingroup$ @LeonidShifrin I didn't know that, I'll definitely keep that in mind. $\endgroup$
    – tlehman
    Commented Jan 5, 2012 at 16:41

5 Answers 5

231
$\begingroup$

Since Mathematica is a symbolic system, with symbolic evaluator much more general than in Matlab, it is not surprising that performance-tuning can be more tricky here. There are many techniques, but they can all be understood from a single main principle. It is:

Avoid full Mathematica symbolic evaluation process as much as possible.

All techniques seem to reflect some facet of it. The main idea here is that most of the time, a slow Mathematica program is such because many Mathematica functions are very general. This generality is a great strength, since it enables the language to support better and more powerful abstractions, but in many places in the program such generality, used without care, can be a (huge) overkill.

I won't be able to give many illustrative examples in the limited space, but they can be found in several places, including some WRI technical reports (Daniel Lichtblau's one on efficient data structures in Mathematica comes to mind), a very good book of David Wagner on Mathematica programming, and most notably, many Mathgroup posts. I also discuss a limited subset of them in my book. I will supply more references soon.

Here are a few most common ones (I only list those available within Mathematica language itself, not mentioning CUDA \ OpenCL, or links to other languages, which are of course also the possibilities):

  1. Push as much work into the kernel at once as possible, work with as large chunks of data at a time as possible, without breaking them into pieces

    1.1. Use built-in functions whenever possible. Since they are implemented in the kernel, in a lower-level language (C), they are typically (but not always!) much faster than user-defined ones solving the same problem. The more specialized version of a built-in function you are able to use, the more chances you have for a speed-up.

    1.2. Use functional programming (Map, Apply, and friends). Also, use pure functions in #-& notation when you can, they tend to be faster than Function-s with named arguments or those based on patterns (especially for not computationally-intensive functions mapped on large lists).

    1.3. Use structural and vectorized operations (Transpose, Flatten, Partition, Part and friends), they are even faster than functional.

    1.4. Avoid using procedural programming (loops etc), because this programming style tends to break large structures into pieces (array indexing etc). This pushes larger part of the computation outside of the kernel and makes it slower.

  2. Use machine-precision whenever possible

    2.1. Be aware and use Listability of built-in numerical functions, applying them to large lists of data rather than using Map or loops.

    2.2. Use Compile, when you can. Use the new capabilities of Compile, such as CompilationTarget->"C", and making our compile functions parallel and Listable.

    2.3. Whenever possible, use vectorized operations (UnitStep, Clip, Sign, Abs, etc) inside Compile, to realize "vectorized control flow" constructs such as If, so that you can avoid explicit loops (at least as innermost loops) also inside Compile. This can move you in speed from Mathematica byte-code to almost native C speed, in some cases.

    2.4. When using Compile, make sure that the compiled function doesn't bail out to non-compiled evaluation. See examples in this MathGroup thread.

  3. Be aware that Lists are implemented as arrays in Mathematica

    3.1. Pre-allocate large lists

    3.2. Avoid Append, Prepend, AppendTo and PrependTo in loops, for building lists etc (because they copy entire list to add a single element, which leads to quadratic rather than linear complexity for list-building)

    3.3. Use linked lists (structures like {1,{2,{3,{}}}} ) instead of plain lists for list accumulation in a program. The typical idiom is a = {new element, a}. Because a is a reference, a single assignment is constant-time.

    3.4. Be aware that pattern-matching for sequence patterns (BlankSequence, BlankNullSequence) is also based on Sequences being arrays. Therefore, a rule {fst_,rest___}:>{f[fst],g[rest]} will copy the entire list when applied. In particular, don't use recursion in a way which may look natural in other languages. If you want to use recursion on lists, first convert your lists to linked lists.

  4. Avoid inefficient patterns, construct efficient patterns

    4.1. Rule-based programming can be both very fast and very slow, depending on how you build your structures and rules, but in practice it is easier to inadvertently make it slow. It will be slow for rules which force the pattern-matcher to make many a priory doomed matching attempts, for example by under-utilizing each run of the pattern-matcher through a long list (expression). Sorting elements is a good example: list//.{left___,x_,middle___,y_,right___}/;x>y:>{left,y,middle,x,right} - has a cubic complexity in the size of the list (explanation is e.g. here).

    4.2. Build efficient patterns, and corresponding structures to store your data, making pattern-matcher to waste as little time on false matching attempts as possible.

    4.3. Avoid using patterns with computationally intensive conditions or tests. The pattern-matcher will give you the most speed when patterns are mostly syntactic in nature (test structure, heads, etc). Every time when condition (/;) or pattern test (?) is used, for every potential match, the evaluator is invoked by the pattern-matcher, and this slows it down.

  5. Be aware of immutable nature of most Mathematica built-in functions

    Most Mathematica built-in functions which process lists create a copy of an original list and operate on that copy. Therefore, they may have a linear time (and space) complexity in the size of the original list, even if they modify a list in only a few places. One universal built-in function that does not create a copy, modifies the original expression and does not have this issue, is Part.

    5.1. Avoid using most list-modifying built-in functions for a large number of small independent list modifications, which can not be formulated as a single step (for example, NestWhile[Drop[#,1]&,Range[1000],#<500&] )

    5.2. Use extended functionality of Part to extract and modify a large number of list (or more general expression) elements at the same time. This is very fast, and not just for packed numerical arrays (Part modifies the original list).

    5.3. Use Extract to extract many elements at different levels at once, passing to it a possibly large list of element positions.

  6. Use efficient built-in data structures

    The following internal data structures are very efficient and can be used in many more situations than it may appear from their stated main purpose. Lots of such examples can be found by searching the Mathgroup archive, particularly contributions of Carl Woll.

    6.1. Packed arrays

    6.2. Sparse arrays

  7. Use hash - tables.

    Starting with version 10, immutable associative arrays are available in Mathematica (Associations)

    7.1 Associations

    the fact that they are immutable does not prevent them to have efficient insertion and deletion of key-value pairs (cheap copies different from the original association by the presence, or absence, of a given key-value pair). They represent the idiomatic associative arrays in Mathematica, and have very good performance characteristics.

    For earlier versions,the following alternatives work pretty well, being based on internal Mathematica's hash-tables:

    7.2. Hash-tables based on DownValues or SubValues

    7.3. Dispatch

  8. Use element - position duality

    Often you can write faster functions to work with positions of elements rather than elements themselves, since positions are integers (for flat lists). This can give you up to an order of magnitude speed-up, even compared to generic built-in functions (Position comes to mind as an example).

  9. Use Reap - Sow

    Reap and Sow provide an efficient way of collecting intermediate results, and generally "tagging" parts you want to collect, during the computation. These commands also go well with functional programming.

  10. Use caching, dynamic programming, lazy evaluation

    10.1. Memoization is very easily implemented in Mathematica, and can save a lot of execution time for certain problems.

    10.2. In Mathematica, you can implement more complex versions of memoization, where you can define functions (closures) at run-time, which will use some pre-computed parts in their definitions and therefore will be faster.

    10.3. Some problems can benefit from lazy evaluation. This seems more relevant to memory - efficiency, but can also affect run-time efficiency. Mathematica's symbolic constructs make it easy to implement.

A successful performance - tuning process usually employs a combination of these techniques, and you will need some practice to identify cases where each of them will be beneficial.

$\endgroup$
21
  • 1
    $\begingroup$ I cannot stress the use of Dispatch enough myself! I frequently go out of my way to use ReplaceAll with a dispatch table if at all possible. $\endgroup$
    – Timo
    Commented Jan 20, 2011 at 7:52
  • 1
    $\begingroup$ @Timo: Dispatch and DownValues-based hashes have somewhat different characteristics. Creating Dispatched definitions for large lists of key-value pairs seems generally faster than with DownValues, and application of Dispatched rules is sometimes also faster. However, once formed, Dispatched hash is not easily appended with new elements, so this is a kind of a one-time affair. With DownValues, you don't face this problem, you can add more key-value pairs at any time without a performance hit. DownValues can also be used to implement a kind of a classic cache, with Sort option set to False $\endgroup$ Commented Jan 20, 2011 at 17:44
  • 1
    $\begingroup$ @Leonid: A really good answer (+100)! Lots of useful hints. You should add a link to this answer in the What is in your Mathematica tool bag question. $\endgroup$
    – Simon
    Commented Mar 16, 2011 at 3:21
  • 1
    $\begingroup$ @TomZinger Glad you found it useful. Re: 10.2: I meant memoizing entire patterns. Have a look at this answer for an example of that, and also this great answer. And also this one - in there, I list the generated definitions, so it is rather explicit in what you get memoized. $\endgroup$ Commented Mar 16, 2016 at 15:48
  • 1
    $\begingroup$ @TomZinger Actually, there is also a section in this answer, called "Memoization for functions of more than one parameter, and using patterns in memoized definitions", which contains a somewhat more accessible explanation of the same thing. See if you find it more clear. $\endgroup$ Commented Mar 17, 2016 at 13:34
51
$\begingroup$

You may use the profiler included in the Wolfram Workbench

alt text

$\endgroup$
3
  • $\begingroup$ I see workbench is free for premium subscribers. Unfortunately, wolfram customer service has been extremely difficult for us to work with. :( We can't get any premium services activated. sigh $\endgroup$
    – John
    Commented Jan 20, 2011 at 4:57
  • 3
    $\begingroup$ Where is the profiler in Workbench? $\endgroup$
    – Eli Lansey
    Commented Feb 16, 2012 at 18:40
  • 7
    $\begingroup$ For the sake of completing the cross-reference: One can profile within Mathematica, too. See Profiling from Mathematica $\endgroup$
    – Michael E2
    Commented Mar 16, 2014 at 12:50
21
$\begingroup$

Take a look at the presentation Principles for Efficient Mathematica Programs from the Wolfram Technology Conference 2007.

Another useful presentation is Tips for Memory Efficient Coding in the Wolfram Language.

$\endgroup$
8
$\begingroup$

If I want to make my code faster, I check if I can use FunctionCompile to speed it up:

https://reference.wolfram.com/language/guide/CodeCompilation.html

This compiler translates WL code into LLVM byte-code, which can then be compiled to native machine code. There is a very good tutorial video on YouTube which explains what you can do with this compiler:

https://www.youtube.com/watch?v=lVdRVUGzJwM

$\endgroup$
0
5
$\begingroup$

Also Data Structures and Efficient Algorithms

Enhance Your Code Performance

The Mathematica Compiler

Effective Use of the Mathematica Compiler and Code Generation

$\endgroup$
1
  • 1
    $\begingroup$ It should be noted that the compiler is going through a serious overhaul leveraging LLVM technology. $\endgroup$
    – rcollyer
    Commented Oct 23, 2021 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.