Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Why would an "end" statement have execution time in the Profiler?

Asked by the cyclist on 5 Nov 2011

The title pretty much says it all. When I profile some code, I notice that the "end" statement of a for loop has non-trivial amounts of execution time. That seems odd to me.

0 Comments

the cyclist

Tags

Products

2 Answers

Answer by Jan Simon on 5 Nov 2011

Matlab checks after every line, if Ctrl-C was pressed (usually...). Going line-by-line through the code using the debugger needs the infmormation about the lines also. Therefore even an end needs some execution time.

Well, this is a pure guessing. What about my 2nd theory:

end is converted implicitely to a goto. This consumes time.

0 Comments

Jan Simon
Answer by Walter Roberson on 5 Nov 2011

There is some overhead for changing the loop variable to the next value. That overhead would normally be small for a regular for loop, but would be higher for a "for" over a matrix such as

for K = rand(3,5)

More likely, though, you are probably seeing JIT effects.

  1. computations that got re-ordered or split up can end up being internally postponed after the computation in the last labeled line of the loop, with the result that they are attributed to the "end" statement
  2. the JIT could hypothetically convert looped lines to vectorized, and the time for that work has to be accounted somewhere
  3. as has been explored in the past, the profiler interacts with the JIT in strange ways, often slowing loops down; attributing time to an "end" statement would then just be more profiler chaos.

2 Comments

the cyclist on 5 Nov 2011

"Profiler chaos" sounds like it might be right. :-) The code also takes almost FIVE times as long to run when being profiled.

Jan Simon on 6 Nov 2011

The JIT accelerator can re-order the commands to increase the speed. This feature is disabled during profiling. I've tested it with some cases only, but in all of them it seems, like the JIT is disabled completely during profiling and debugging.
I think, that the time for advancing the loop counter is spend in the FOR line, not in the END. If Matlab would be C the END would contain the check for reaching the end of the loop.
Again: This is *not* based on scientific knowledge of the interna.

Walter Roberson

Contact us