为什么 Matlab Profiler 说“for"循环的“end"语句存在瓶颈? [英] Why does the Matlab Profiler say there is a bottleneck on the 'end' statement of a 'for' loop?
问题描述
所以,我最近开始定期使用 Matlab 的内置分析器,我注意到虽然它通常能很好地显示哪些行占用的时间最多,但有时它会告诉我一个很大的在 for
循环的 end
语句上使用了大量时间.
So, I've recently started using Matlab's built-in profiler on a regular basis, and I've noticed that while its usually great at showing which lines are taking up the most time, sometimes it'll tell me a large chunk of time is being used on the end
statement of a for
loop.
现在,看到这样的一行只是用于表示循环的结束,我无法想象除了微不足道的处理量之外,它还能如何使用任何东西.
Now, seeing as such a line is just used for denoting the end of the loop, I can't imagine how it could use anything other than a trivial amount of processing.
我在 matlab central,但似乎没有达成共识.
I've seen a specific version of this question asked on matlab central, but a consensus didn't seem to be reached.
这是此问题的一个最小示例:
Here's a minimal example of this problem:
for i =1:1000
x = 1;
x = [x 1];
% clear x;
end
即使取消注释 clear
,end
行仍然占用大量计算(约 20%),而 clear
实际上增加了结束行执行的绝对计算量.
Even if you uncomment the clear
, the end
line still takes up a lot of computation (about 20%), and the clear
actually increases the absolute amount of computation performed by the end line.
推荐答案
当我在我的代码中看到这一点时,它是在循环中创建的大型临时对象的释放.在循环中创建的每个新变量都在 end
处释放.
When I've seen this in my code, it's been the deallocation of large temporaries created in the loop. Each new variable created in the loop is deallocated at the end
.
这篇关于为什么 Matlab Profiler 说“for"循环的“end"语句存在瓶颈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!