Why does my one calculation take so much longer than the other when its the same function?

4 views (last 30 days)
So I'm running orbital simulation code and its taking a lot longer than I'd like. The part that takes the most is the integration function, stepxy.
for i = 2:1e6
ploot(i,:)=stepxy(ploot(i-1,:),t);
end
In stepxy, nothing takes too long except the very last line which was "end", so I deleted it, as per Matlab's suggestion involving "ends" in functions, and now the last line is taking way too long.
0.56 seconds for dxen= i(7) + (-G*Ms*i(5)/sqrt(i(5)^2+i(6)^2)^3)*t; then right after it
3.16 seconds for dyen= i(8) + (-G*Ms*i(6)/sqrt(i(5)^2+i(6)^2)^3)*t;
whats going on?? there is no difference in variable type or size or scale between the two lines.
  3 Comments
James Tursa
James Tursa on 13 Apr 2016
We would probably need to see more of your code. But be advised that the profiler can get confused sometimes as to where the time is being spent. So while the overall time might be correct, the piecing out of the timings to the individual lines can be somewhat random in some cases. E.g., assigning all of the time to the "end" line. Deleting that line only changes where that confused timing gets assigned ... it doesn't really affect the overall timing. That could be what is happening in your case.
jgg
jgg on 13 Apr 2016
You could try adding a bunch of tic toc commands in the problematic function if the profiler isn't doing a great job.

Sign in to comment.

Answers (1)

Philip Borghesani
Philip Borghesani on 13 Apr 2016
When end at the end of a function shows a significant amount time the cause is most often the overhead involved in cleaning up the workspace before exiting the function. removing the end statement will only move this time to the preceding statement. Without seeing the whole function and knowing the contents of any cell arrays or structures there is no way to guess why the cleanup is taking an appreciable amount of time.
This is most often caused by large structures or cell arrays containing cycles. Handle object are frequently involved in such loops.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!