本文介绍了Cellfun与简单Matlab Loop的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间,我在大学里开始使用matlab时,如果我的主管看到任何不必要的for循环(他会要求将其交换为kron或尽可能进行任何类型的索引操作),我会杀了我.后来,我试图尽可能避免在matlab上出现任何循环,而是寻找最暗的matlab编码方式来做黑魔法,而不是简单的循环.

When I started working with matlab sometime ago in the university, my supervisor would kill me if he saw any unnecessary for loop (he would ask for exchanging it for kron or any type of indexes manipulation as possible). Later on, I was trying to avoid as much as possible any loop on matlab, searching for the darkest matlab coding ways to do black magic instead of a simple loop.

有一天,我发现了cellfun,它使黑魔法变得更加简单,我可以更改使用单元格和cellfun组合的许多循环,但是有一天,我看到了一个有关cellfun的帖子,这使我质疑我继承的matlab知识是否正确,即: matlab循环总是比内置的编译函数慢,这是我非常有信心的事情.我在一种实现中对其进行了测试,实际上, for循环会更快!我就像,天哪,那几天做晦涩难懂的代码是白白浪费了哈哈哈.从那天开始,我就不再努力尝试优化matlab代码,通常情况取决于每种情况,依此类推.

And one day I discovered the cellfun, which made the black magic quite simplier, I could change many loops working with cells and cellfun combo, but one day I saw one post about cellfun which made me question if my inherited matlab knowledge was true, that is: that matlab loops would always be slower than one builtin compiled function, which was something I had so much faith. I tested it in one of my implementations and in fact, the for loop would be faster! I was like, OMG, all those days doing obscure code wasted for nothing hahaha. Since that day, I stopped working that hard to try to optimize matlab code, normally it depends for each case and so on.

今天我看到了这个答案,使我想起了我为避免尽可能多的matlab循环所做的努力(我不知道作者是否会避免这样做,但是无论如何,它提醒了所有这些matlab循环性能问题.)我想到一个问题: cellfun是否比for循环好?什么时候是真的?

Today I saw this answer, which remembered my effort for avoiding as much matlab loops as possible (I don't know if that was the author will to avoid for performance, but anyway it reminded all this matlab loop performance thing). And one question came to my mind: Is cellfun better than for loops? When would that be true?

推荐答案

如果性能是主要因素,则应避免使用单元格,循环或cellfun/arrayfun.通常,使用向量运算会更快(假设可以这样做).

If performance is a major factor you should avoid using cells, loops or cellfun/arrayfun.It's usually much quicker to use a vector operation (assuming this is possible).

下面的代码在Werner的add示例中进行了扩展,该示例具有标准的数组循环和数组操作.

The code below expands on Werner's add example with standard array loop and array operations.

结果是:

  • 单元格循环时间-0.1679
  • Cellfun时间-2.9973
  • 循环数组时间-0.0465
  • 数组时间-0.0019

代码:

nTimes = 1000;
nValues = 1000;
myCell = repmat({0},1,nValues);
output = zeros(1,nValues);

% Basic operation
tic;
for k=1:nTimes
  for m=1:nValues
    output(m) = myCell{m} + 1;
  end
end
cell_loop_timeAdd=toc;
fprintf(1,'Cell Loop Time %0.4f\n', cell_loop_timeAdd);

tic;
for k=1:nTimes
  output = cellfun(@(in) in+1,myCell);
end
cellfun_timeAdd=toc;
fprintf(1,'Cellfun Time %0.4f\n', cellfun_timeAdd);


myData = repmat(0,1,nValues);
tic;
for k=1:nTimes
  for m=1:nValues
    output(m) = myData(m) + 1;
  end
end
loop_timeAdd=toc;
fprintf(1,'Loop Array Time %0.4f\n', loop_timeAdd);

tic;
for k=1:nTimes
    output = myData + 1;
end
array_timeAdd=toc;
fprintf(1,'Array Time %0.4f\n', array_timeAdd);

这篇关于Cellfun与简单Matlab Loop的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:14
查看更多