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

2 次查看(过去 30 天)
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 个评论
James Tursa
James Tursa 2016-4-13
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 2016-4-13
You could try adding a bunch of tic toc commands in the problematic function if the profiler isn't doing a great job.

请先登录,再进行评论。

回答(1 个)

Philip Borghesani
Philip Borghesani 2016-4-13
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.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by