array/cellfun vs. for loop
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
5 投票
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
2012 年 6 月 28 日
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
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
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
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
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
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
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
2018 年 8 月 20 日
I do find the blog article at the link indicated.
その他の回答 (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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
