Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the Matlab Profiler say there is a bottleneck on the 'end' statement of a 'for' loop?

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.

like image 833
zergylord Avatar asked Aug 01 '11 23:08

zergylord


1 Answers

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.

like image 135
Marc Avatar answered Nov 10 '22 09:11

Marc