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.)?

 采纳的回答

Walter Roberson
Walter Roberson 2012-6-28

6 个投票

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 个评论

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
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.
@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/
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.
I do find the blog article at the link indicated.
Strange. Maybe it was offline temporarily, or my own connection had a hiccup. Anyway, here is a permalink for future reference.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by