MATLAB vs Python: finite difference





We were doing a homework question with coding components, part of which involved taking derivatives. Being in an engineering lab, the go-to language is often MATLAB. Unaware of the much more straightforward diff function which my classmate used, I constructed a finite difference matrix using spdiags. As a result, the code was several lines longer, as I needed to create a few matrices and arrays as inputs to the spdiags function. On top of that, there was a matrix multiplication at the end instead of a straightforward array manipulations. Out of curiosity, I timed the execution time of both code on my laptop. To my amazement, the matrix formulation easily won; I tested each code multiple times to account for random machine variance over time, and not a single time did the other code execute faster. Now my curiosity was really peaked.

Anyone who have used MATLAB knows how slow it is. This, among other reasons, makes people rightly conclude that it is not a real programming language. Mathworks never claimed it to be a top choice for app programmers, but it has been selling MATLAB as a scientific computing language. In particular, it has been said that its algorithms that deal with matrices is a big selling point. But what were the hard numbers surrounding this claim? Numbers were not readily available (although digging a little deeper, some material could be found online), so I thought to do a few of my own tests.

My tests compared four finite difference methods on two languages: built-in difference function, matrix, simple convolution, and Gaussian convolution methods. I have never seen the last two methods, but they seem to be common in image processing community due to easy implementation. Quick explanation: the difference in finite difference could be seen as a convolution of the original function with the array [1, -1] - the simple convolution method. Convolution with a Gaussian window applies Gaussian smoothing to the signal to obtain smoother curves. I coded in MATLAB and Python, a language I was recently learning and which I found to be both quick and nimble. I ran the code a dozen times or so, threw out the outliers, and found the mean.

The Python distribution tested is the popular Enthought Canopy. This comparison is particularly meaningful because Canopy is also marketed as a scientific computing platform generally obtains good reviews.

The following table tells an interesting story:
The units are in milliseconds. MATLAB gets easily beaten by most of the markers here. The only MATLAB functionality that won out is its matrix methods. As mentioned earlier, its optimized matrix algorithms allows for faster computation, although setting up the formulation in MATLAB takes some effort. After the first two times using spdiags I find it becomes straightforward. I suppose this difficulty just makes those who knows how to do it so much more valuable. Matrices are known to be computationally intensive, so for an un-optimized language like python, it is not surprising to find it struggling the most with matrix operations.

MATLAB does not have special functions for Gaussian filters. MATLAB implementations of Gaussian convolution, therefore, would use the same conv function as the simple convolution method, guaranteeing an execution time no faster than simple convolution.

If one is just looking to write pure numerical code, Python seems a good choice, unless there is a heavy amount of linear algebra involved. For many users, however, other functionalities are also needed. MATLAB's plethora of toolboxes are nice, but with some work, one could usually find modules with similar functions in Python. For my homework, I needed to present my computed results using graphs. Here is the result when I timed my entire code for the assignments:
This is something that I intuitively knew. MATLAB plots seemed to pop-up just a tad slower than Python plots, and this is now evident in the tests.

These tests illustrated both the weaknesses and strengths of this computing language. MATLAB is great for doing hard-core linear algebra work, and to a lesser extent, general numerical work, but as soon as it begins to get fancy, be it adding a GUI or producing pretty pictures, it may be better to look elsewhere.