There are a few articles that show that MATLAB prefers column operations than row operations, and that depending on you lay out your data the performance can vary significantly. This is apparently because MATLAB uses a column-major order for representing arrays.
I remember reading that Python (NumPy) uses a row-major order. With this, my questions are:
- Can one expect a similar difference in performance when working with NumPy?
- If the answer to the above is yes, what would be some examples that highlight this difference?
I suspect it will differ depending on the data and the operations.
The easy answer is to write some tests using the same, real world, data of the sort you are planning on using and the functions that you are planning on using and then use
cprofile
ortimeit
to compare the speeds, for your operations, depending on how you structure your data.Like many benchmarks, this really depends on the particulars of the situation. It's true that, by default, numpy creates arrays in C-contiguous (row-major) order, so, in the abstract, operations that scan over columns should be faster than those that scan over rows. However, the shape of the array, the performance of the ALU, and the underlying cache on the processor have a huge impact on the particulars.
For instance, on my MacBook Pro, with a small integer or float array, the times are similar, but a small integer type is significantly slower than the float type:
With larger arrays the absolute differences become larger, but at least on my machine are still smaller for the larger datatype:
You can tell numpy to create a Fortran-contiguous (column-major) array using the
order='F'
keyword argument tonumpy.asarray
,numpy.ones
,numpy.zeros
, and the like, or by converting an existing array usingnumpy.asfortranarray
. As expected, this ordering swaps the efficiency of the row or column operations: