# Compiling Wolfram Language Expressions

If you make a definition like f[x_]:=x Sin[x], the Wolfram Language will store the expression x Sin[x] in a form that can be evaluated for any . Then when you give a particular value for , the Wolfram Language substitutes this value into x Sin[x], and evaluates the result. The internal code that the Wolfram Language uses to perform this evaluation is set up to work equally well whether the value you give for is a number, a list, an algebraic object, or any other kind of expression.

Having to take account of all these possibilities inevitably makes the evaluation process slower. However, if the Wolfram Language could *assume* that will be a machine number, then it could avoid many steps, and potentially evaluate an expression like x Sin[x] much more quickly.

Using Compile, you can construct *compiled functions* in the Wolfram Language, which evaluate Wolfram Language expressions assuming that all the parameters which appear are numbers (or logical variables). Compile[{x_{1},x_{2},…},expr] takes an expression expr and returns a "compiled function" which evaluates this expression when given arguments .

In general, Compile creates a CompiledFunction object which contains a sequence of simple instructions for evaluating the compiled function. The instructions are chosen to be close to those found in the machine code of a typical computer, and can thus be executed quickly.

Compile[{x_{1},x_{2},…},expr] | create a compiled function which evaluates expr for numerical values of the |

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

Compile is useful in situations where you have to evaluate a particular numerical or logical expression many times. By taking the time to call Compile, you can get a compiled function which can be executed more quickly than an ordinary Wolfram Language function.

For simple expressions such as x Sin[x], there is usually little difference between the execution speed for ordinary and compiled functions. However, as the size of the expressions involved increases, the advantage of compilation also increases. For large expressions, compilation can speed up execution by a factor as large as 20.

Compilation makes the biggest difference for expressions containing a large number of simple, say arithmetic, functions. For more complicated functions, such as BesselK or Eigenvalues, most of the computation time is spent executing internal Wolfram Language algorithms, on which compilation has no effect.

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

Even though you can use compilation to speed up numerical functions that you write, you should still try to use built‐in Wolfram Language functions whenever possible. Built‐in functions will usually run faster than any compiled Wolfram Language programs you can create. In addition, they typically use more extensive algorithms, with more complete control over numerical precision and so on.

You should realize that built‐in Wolfram Language functions quite often themselves use Compile. Thus, for example, NIntegrate by default automatically uses Compile on the expression you tell it to integrate. Similarly, functions like Plot and Plot3D use Compile on the expressions you ask them to plot. Built‐in functions that use Compile typically have the option Compiled. Setting Compiled->False tells the functions not to use Compile.

Compile[{{x_{1},t_{1}},{x_{2},t_{2}},…},expr] | compile expr assuming that is of type |

Compile[{{x_{1},t_{1},n_{1}},{x_{2},t_{2},n_{2}},…},expr] | |

compile expr assuming that is a rank array of objects each of type | |

Compile[vars,expr,{{p_{1},pt_{1}},…}] | compile expr, assuming that subexpressions which match are of type |

_Integer | machine‐size integer |

_Real | machine‐precision approximate real number |

_Complex | machine‐precision approximate complex number |

TrueFalse | logical variable |

Specifying types for compilation.

Compile works by making assumptions about the types of objects that occur in evaluating the expression you give. The default assumption is that all variables in the expression are approximate real numbers.

Compile nevertheless also allows integers, complex numbers and logical variables (True or False), as well as arrays of numbers. You can specify the type of a particular variable by giving a pattern which matches only values that have that type. Thus, for example, you can use the pattern to specify the integer type. Similarly, you can use TrueFalse to specify a logical variable that must be either True or False.

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

The types that Compile handles correspond essentially to the types that computers typically handle at a machine‐code level. Thus, for example, Compile can handle approximate real numbers that have machine precision, but it cannot handle arbitrary‐precision numbers. In addition, if you specify that a particular variable is an integer, Compile generates code only for the case when the integer is of "machine size", typically between .

When the expression you ask to compile involves only standard arithmetic and logical operations, Compile can deduce the types of objects generated at every step simply from the types of the input variables. However, if you call other functions, Compile will typically not know what type of value they return. If you do not specify otherwise, Compile assumes that any other function yields an approximate real number value. You can, however, also give an explicit list of patterns, specifying what type to assume for an expression that matches a particular pattern.

In[11]:= |

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

The idea of Compile is to create a function which is optimized for certain types of arguments. Compile is nevertheless set up so that the functions it creates work with whatever types of arguments they are given. When the optimization cannot be used, a standard Wolfram Language expression is evaluated to find the value of the function.

In[14]:= |

Out[14]= |

In[15]:= |

Out[15]= |

In[16]:= |

Out[16]= |

The compiled code generated by Compile must make assumptions not only about the types of arguments you will supply, but also about the types of all objects that arise during the execution of the code. Sometimes these types depend on the actual *values* of the arguments you specify. Thus, for example, Sqrt[x] yields a real number result for real x if x is not negative, but yields a complex number if x is negative.

Compile always makes a definite assumption about the type returned by a particular function. If this assumption turns out to be invalid in a particular case when the code generated by Compile is executed, then the Wolfram Language simply abandons the compiled code in this case, and evaluates an ordinary Wolfram Language expression to get the result.

In[17]:= |

Out[17]= |

An important feature of Compile is that it can handle not only mathematical expressions, but also various simple Wolfram Language programs. Thus, for example, Compile can handle conditionals and control flow structures.

In all cases, Compile[vars,expr] holds its arguments unevaluated. This means that you can explicitly give a "program" as the expression to compile.

In[18]:= |

Out[18]= |

In[19]:= |

Out[19]= |