Matlab:arrayfun,cellfun,spfun和structfun与简单的for-loop [英] Matlab: arrayfun, cellfun, spfun and structfun vs. simple for-loop

查看:182
本文介绍了Matlab:arrayfun,cellfun,spfun和structfun与简单的for-loop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用所有 * fun 函数( arrayfun cellfun , structfun spfun )或者简单地使用 code> loop?



什么方法可以提供更好的性能,哪些方法应该被认为是更好的代码可读性的实践? b

解决方案

这真的取决于你所说的'表现':) b
$ b

如果你的意思是最小执行时间,有时 * fun 更快(例如, cellfun('isempty',...); (是的,字符串参数!)确实击败循环版本)。有时一个循环更快。如果你使用的是Matlab版本< 2006年,默认情况下去 * fun 函数。如果你更近一些,默认情况下去循环。你仍然需要简介,找出哪一个更快。正如Amro所指出的,如果你有一个能够执行FP算法的GPU和一个支持GpGPU的最新版本的Matlab,那么调用 arrayfun 用于 gpuArray 输入将被大量并行化。但是,关于执行时间,不能一概而论;对于更小的阵列,或绝对humungous的,复制一切到GPU的开销可能会取消任何并行计算的好处,所以...分析是真正知道的唯一方法。

如果你的意思是 minimum code ,那么我认为按照 * fun 只要操作简单。对于任何复杂的事情来说,往往是更好的循环。如果你的意思是最佳可读性,那么在专业环境下维护和实施变更所需的最短时间,当然,去循环。



在这个时候,对于你的问题并没有一个明确的简单答案:)

Which one is better, using all the *fun functions (arrayfun, cellfun, structfun and spfun) or simply using for loop?

What method gives better performance and which methods should be considered better practice in terms of readability of the code?

解决方案

It really depends on what you call 'performance' :)

If you mean minimum execution time, well, sometimes *fun are faster (for example, cellfun('isempty', ...); (yes, string argument!) for sure beats the loop version). Sometimes a loop is faster. If you're on a Matlab version < 2006, go for the *fun functions by default. If you're on anything more recent, go for the loops by default. You'll still always have to profile to find out which one's faster.

As noted by Amro, if you have a GPU capable of doing FP arithmetic, and a recent version of Matlab that supports GpGPU, then a call to arrayfun for gpuArray inputs will be massively-parallelized. However, no general statements can be made regardnig execution time; for smaller arrays, or absolutely humungous ones, the overhead of copying everything over to the GPU might undo any benefit of parallelizing the computations, so...profiling is really the only way to know for sure.

If you mean minimum coding time, then I'd say it's usually faster to code in terms of *fun as long as the operations are simple. For anything complex it's usually better to go for the loop.

If you mean optimum readability and thus minimum time required for maintenance and implementation of changes in a professional context, for sure, go for the loop.

At this point in time, there's not really a clear-cut simple answer to your question :)

这篇关于Matlab:arrayfun,cellfun,spfun和structfun与简单的for-loop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆