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 件のコメント

Sean de Wolski
Sean de Wolski 2012 年 6 月 28 日
Well said, +1. Though I know longer need to say '+1' because the eyes in the sky know it's me!
Tom
Tom 2012 年 6 月 28 日
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 2012 年 6 月 28 日
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!
Mark
Mark 2016 年 4 月 23 日
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 2016 年 7 月 26 日
@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 2018 年 8 月 20 日
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 2018 年 8 月 20 日
I do find the blog article at the link indicated.
Rik
Rik 2018 年 8 月 20 日
Strange. Maybe it was offline temporarily, or my own connection had a hiccup. Anyway, here is a permalink for future reference.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by