Why ist my variable allocation so slow?
3 次查看(过去 30 天)
显示 更早的评论
At the beginning of my function i am assigning values to variables.
When analysing the function with the profiler, it seems that two lines are are a lot slower that the rest. Does anybody know the reason for this? How can I speed up these lines? 

I do not understand thes especialy since the contents of the varibles pn and an (lines 22 and 19) are quite similar, but the time needed is 50 times slower. An example or an and pn are:


How chan the time needed be so different?
2 个评论
Rik
2021-4-23
The results for an and pn might be similar, but why would that mean the time required to get there would be similar? Those two lines look like they're doing very different things. Indexing several layers deep is bound to get slow at some point. You could try storing each step in an intermediate variable to see which of these step are the true bottleneck.
回答(1 个)
Jan
2021-4-23
Do not overestimate the accuracy of the profiler. Sometimes the END uses a lot of time in a FOR loop, although no work is done there.
Matlab's JIT can reorder commands and re-use deeply nested arrays. This confuses the profiler massively and important or all parts of the JIT acceleration are switched off for profiling. So you have to interprete the results with care.
The code can be accelerated by avoiding repeated access of deeply nested structs. Please post the corresponding code, such that we can suggest improvements by copy&paste.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!