User guide
NumPy User Guide, Release 1.9.0
asked for (and the array types are the same), the already- compiled shared library will be loaded and used. Because
Blitz makes extensive use of C++ templating, it can take a long time to compile the first time. After that, however, the
code should evaluate more quickly than the equivalent NumPy expression. This is especially true if your array sizes
are large and the expression would require NumPy to create several temporaries. Only expressions involving basic
arithmetic operations and basic array slicing can be converted to Blitz C++ code.
For example, consider the expression:
d = 4
*
a + 5
*
a
*
b + 6
*
b
*
c
where a, b, and c are all arrays of the same type and shape. When the data-type is double-precision and the size
is 1000x1000, this expression takes about 0.5 seconds to compute on an 1.1Ghz AMD Athlon machine. When this
expression is executed instead using blitz:
d = empty(a.shape, ’d’); weave.blitz(expr)
execution time is only about 0.20 seconds (about 0.14 seconds spent in weave and the rest in allocating space for d).
Thus, we’ve sped up the code by a factor of 2 using only a simnple command (weave.blitz). Your mileage may vary,
but factors of 2-8 speed-ups are possible with this very simple technique.
If you are interested in using weave in this way, then you should also look at scipy.numexpr which is another similar
way to speed up expressions by eliminating the need for temporary variables. Using numexpr does not require a C/C++
compiler.
Inline C-code
Probably the most widely-used method of employing weave is to “in-line” C/C++ code into Python in order to speed
up a time-critical section of Python code. In this method of using weave, you define a string containing useful C-code
and then pass it to the function weave.inline ( code_string, variables ), where code_string is a string of valid
C/C++ code and variables is a list of variables that should be passed in from Python. The C/C++ code should refer
to the variables with the same names as they are defined with in Python. If weave.line should return anything the the
special value return_val should be set to whatever object should be returned. The following example shows how to use
weave on basic Python objects:
code = r"""
int i;
py::tuple results(2);
for (i=0; i<a.length(); i++) {
a[i] = i;
}
results[0] = 3.0;
results[1] = 4.0;
return_val = results;
"""
a = [None]
*
10
res = weave.inline(code,[’a’])
The C++ code shown in the code string uses the name ‘a’ to refer to the Python list that is passed in. Because the
Python List is a mutable type, the elements of the list itself are modified by the C++ code. A set of C++ classes are
used to access Python objects using simple syntax.
The main advantage of using C-code, however, is to speed up processing on an array of data. Accessing a NumPy
array in C++ code using weave, depends on what kind of type converter is chosen in going from NumPy arrays to C++
code. The default converter creates 5 variables for the C-code for every NumPy array passed in to weave.inline. The
following table shows these variables which can all be used in the C++ code. The table assumes that myvar is the
name of the array in Python with data-type {dtype} (i.e. float64, float32, int8, etc.)
5.2. Using Python as glue 67