Fastest way for page-wise computation - FOR vs ARRAYFUN vs PAGEFUN

I'd like to know the fastest way to deal with a 3D array in a page-wise way. Suppose I have the following data:
rng(0);
data = randi([1, 10], 10, 10, 1000); % sample data
and I want to know the inverse matrix of each page of this data. I compared three approaches to compute this:
% Approach1 - FOR loop
tic;
data_inv1 = zeros(10,10,1000);
for ii = 1:size(data,3)
data_inv1(:,:,ii) = inv(data(:,:,ii));
end
time1 = toc;
% Approach2 - Arrayfun
tic;
data_inv2 = arrayfun(@(ii)inv(data(:,:,ii)), 1:size(data,3), 'UniformOutput', false);
time2 = toc;
% Approach3 - Pagefun
tic;
data_gpu = gpuArray(data);
data_inv3 = pagefun(@inv, data_gpu);
time3 = toc;
% Comparison of elapsed times
fprintf('FOR-loop: %.8f sec\n', time1);
fprintf('Arrayfun: %.8f sec\n', time2);
fprintf('Pagefun: %.8f sec\n', time3);
The results on my computer are shown below:
FOR-loop: 0.01121580 sec
Arrayfun: 0.01491200 sec
Pagefun: 0.01317000 sec
Surprisingly, the use of the for-loop was the fastest. I expected that pagefun will give me the fastest computation but that was not the case.
Has anyone tried different approaches to speedup the page-wise computation? Is there anyway to make a good use of pagefun?

4 commentaires

Why the surprise?
The for loop is dead-ahead code with no overhead and no function handles.
The alternatives are higher-level abstractions for the convenience of writing code, but that convenicence comes with a cost.
This is a pretty general conclusion; if you can't directly vectorize something immediately to get into the use of buitin code, the plainest, most straightforward code you can write will generally also be the fastest.
"... with no overhead ..."
There is overhead involved with the above code. Namely the deep data copying of the pages. This can potentially be avoided in a mex routine.
Yes...sorry, I intended as the additional overhead with the higher-level constructs, not the inherent memcopy of addressing planes.
In theory, MATLAB should be able to address data(:,:,ii) in place without the explicit copy.
What GPU do you have? I suspect pagefun will be faster if you process in single precision.

Connectez-vous pour commenter.

Réponses (0)

Catégories

Produits

Version

R2020a

Tags

Question posée :

le 6 Mai 2022

Commenté :

le 9 Mai 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by