For a benchmark comparison, I consider the simple function:
function dealiasing2d(where_dealiased, data)
[n1, n0, nk] = size(data);
for i0=1:n0
for i1=1:n1
if where_dealiased(i1, i0)
data(i1, i0, :) = 0.;
end
end
end
It can be useful in pseudo-spectral simulations (where data
is a 3d array of complex numbers) but basically it applies a mask to a set of images, putting to zeros some elements for which where_dealiased
is true.
I compare the performance of different languages (and implementations, compilers, ...) on this simple case. For Matlab, I time the function with timeit. Since I don't want to benchmark my ignorance in Matlab, I would like to really optimize this function with this language. What would be the fastest way to do this in Matlab?
The simple solution I use now is:
function dealiasing2d(where_dealiased, data)
[n1, n0, nk] = size(data);
N = n0*n1;
ind_zeros = find(reshape(where_dealiased, 1, []));
for ik=1:nk
data(ind_zeros + N*(ik-1)) = 0;
end
I suspect this is not the right way to do it since the equivalent Numpy solution is approximately 10 times faster.
import numpy as np
def dealiasing(where, data):
nk = data.shape[0]
N = reduce(lambda x, y: x*y, data.shape[1:])
inds, = np.nonzero(where.flat)
for ik in xrange(nk):
data.flat[inds + N*ik] = 0.
Finally, if someone tells me something like "When you want to be very fast with a particular function in Matlab, you should compile it like that: [...]", I would include such solution in the benchmark.
Edit:
After 2 answers, I've benchmarked the propositions and it seems that there is no noticeable performance improvement. This is strange since the simple Python-Numpy solution is really (one order of magnitude) much faster so I am still looking for a better solution with Matlab...