array/cellfun vs. for loop

Are arrayfun and cellfun always faster than functionally equivalent for loops? If so, why? (E.g., is it a difference in the library functions they call for implementation?) Finally, is it possible to give a general "order function" by which they're faster (e.g., O(N), O(NlogN), etc.)?

 Risposta accettata

Walter Roberson
Walter Roberson il 28 Giu 2012

6 voti

For loops are usually faster than arrayfun or cellfun, as the for loop does not need to invoke the function handle each time. The for loop also has opportunities for optimizations between statements that the arrayfun or cellfun would not have.
arrayfun() or cellfun() can be faster to write the code for, as they are a higher level concept. Not always, though: some of the twists one has to go through to create the behaviour as an anonymous function can be messy.

8 Commenti

Sean de Wolski
Sean de Wolski il 28 Giu 2012
Well said, +1. Though I know longer need to say '+1' because the eyes in the sky know it's me!
It's also worth mentioning that if you're just invoking a built-in function, it can be quicker to write the name as a string rather than use a function handle. e.g.
function HandleTest
Filem=regexp(repmat(cellstr(ls),50,1),'\.m');
tic
FmC=cellfun(@isempty,Filem);
toc
tic
FmC=cellfun('isempty',Filem);
toc
David Goldsmith
David Goldsmith il 28 Giu 2012
Wow, I was assuming (as one can tell from the phrasing of my Q) that the opposite was true, so I'm glad I asked! Thanks!
For the example Tom gave, however, cellfun with string is much faster than a for loop (in answer to the original question).
>> Filem=regexp(repmat(cellstr(ls),1e4,1),'\.m');
>> NF=length(Filem);
>> tic; FmC=cellfun(@isempty,Filem); toc
Elapsed time is 0.755874 seconds.
>> tic;FmC=zeros(NF,1);for n=1:NF; FmC(n)=isempty(Filem{n});end ;toc
Elapsed time is 0.913470 seconds.
>> tic; FmC=cellfun('isempty',Filem); toc
Elapsed time is 0.021828 seconds.
Hoi Wong
Hoi Wong il 26 Lug 2016
@TOM: Be careful about that 'built-in' functor for cellfun(). I got burned by it before. It only works correctly for low level native data types. Details: http://wonghoi.humgar.com/blog/2016/07/23/matlabs-cellfun-high-performance-trap/
Rik
Rik il 20 Ago 2018
That blog post seems offline and the Wayback Machine doesn't have a copy. The doc does contain some warnings if you have some fancy class you want to apply it to:
If you specify a function name rather than a function handle:
  • cellfun does not call any overloaded versions of the function.
  • The size and isclass functions require additional inputs to the cellfun function:
A = cellfun('size',C,k) returns the size along the kth dimension of each element of C.
A = cellfun('isclass',C,classname) returns logical 1 (true) for each element of C that matches the classname argument. This syntax returns logical 0 (false) for objects that are a subclass of classname.
Walter Roberson
Walter Roberson il 20 Ago 2018
I do find the blog article at the link indicated.
Rik
Rik il 20 Ago 2018
Strange. Maybe it was offline temporarily, or my own connection had a hiccup. Anyway, here is a permalink for future reference.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by