Compiling Your Program
Note
We’ve come to the end of the programming tips to make your code run faster. The rest of this page offers methods of compiling your code in ways that can increase its speed. Note that I have not personally explored the Python-based options.
Python
If you want the friendliness of the Python language syntax, but the speed of a compiled language, there are compilers for Python.
These are installed on the Nevis particle-physics Linux cluster. If you visit the links below, you can ignore the installation instructions if you’re using one of the computers on the cluster, or you’re using The Notebook Server.
Numba
Numba is a “just-in-time” compiler for Python. Basically, you create some code within a Python decorator that Numba will compile the first time the decorated function is called. After the compilation, the function will execute at C-level speed.
If you went through The RDataframe Path to the end, you’ve already seen a practical example of using Numba.
Cython
Cython is both a compiler and a language extension for Python.
In general, you’ll want to use Cython from the command line, as you would C++. But you can use it on our notebook server. The Nevis wiki Jupyter page has an example.
C++
On the Nevis particle-physics Linux cluster, the GNU C++ compiler is standard. Its major competitor is clang.
By default, both compilers produce code that is optimized for
debugging. But once you’re convinced that your code works, you may
want to get some extra speed by setting the -O3
option, which
optimizes the code for execution speed.1
For example:2
g++ `root-config --cflags --libs` analysis.cc -o analysis -O3

Figure 108: https://xkcd.com/664/ by Randall Munroe
- 1
Why should optimizing the code for speed make it harder to debug? Take another look at Listing 54, the example of “bad” C++ code. I described how the code computed values more than once, even though their values were unchanged in the loop. An optimizing compiler would pull out any code that was independent of the loop, and execute it before the loop started.
However, it would do this on the machine-language level (or on the LLVM level, a topic way beyond the scope of both this tutorial and this footnote). There would no longer be a correpondence between a bit of code that the computer executed and a given line in your program. This means that any error messages generated during the execution of your program would be harder to track down; an error message could not include text like “segmentation fault in line 17”.
- 2
Did I just give away an answer to a question I posed back in Exercise 11? Oops!