问题
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.
I've seen a specific version of this question asked on matlab central, but a consensus didn't seem to be reached.
EDIT: Here's a minimal example of this problem:
for i =1:1000
x = 1;
x = [x 1];
% clear x;
end
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.
回答1:
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
.
来源:https://stackoverflow.com/questions/6905809/why-does-the-matlab-profiler-say-there-is-a-bottleneck-on-the-end-statement-of