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.)?
采纳的回答
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!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
