MATLAB's power function to calculate element-wise exponential for a constant base and an array of exponents becomes noticeably faster when the size of the array becomes 512. I expected to see the computation time increase with the input size, however, there is a noticeable drop when there are 512 elements in the array of exponents. Here is a sample code
x_list = 510:514;
for i = 1:numel(x_list)
x = x_list(i);
tic
for j = 1:10000
y = power(2,1:x);
end
toc
end
The output of the code is
Elapsed time is 0.397649 seconds.
Elapsed time is 0.403687 seconds.
Elapsed time is 0.318293 seconds.
Elapsed time is 0.238875 seconds.
Elapsed time is 0.175525 seconds.
What is happening here?
tic/toc
is not recommended when doing accurate timings; instead, usetimeit()
, which has been designed to accurately assess performance. Otherwise, I think this has to do with the implementation of FFTW, which also uses a trick to compute very fast on sizes of2^n
, although I'd expect a speed-up for 1024 and 2048 elments then as well. – Livelihoodtimeit
. – Sayed