I am trying to improve the performance of a script and am seeing something I don’t understand.
The script takes around 1600 seconds to complete and the vast, vast majority of that is time spent plotting (the script runs most of a simulation and then loops on certain parameters in order to produce frames for an animation). The simulation performance is very good but the plotting is very slow. Anyway…
Running the profiler I see that ~16% of total time (over 300 seconds) are spent in pause(). I never call pause() in any of my code. Is this normal for plot()/text() related calls? If not am I doing something wrong; is there something I try that might improve plot()/text() performance?
It’s not a big deal but I would like to optimize this as much as possible since once I get everything working as I like and I bump the simulation up to higher resolution this is going to get much worse.
configured for x86_64-w64-mingw32
octave-7.1.0-w64-installer.exe downloaded from gnu.org
Up to date Windows 10.
Are you calling
Can you post your code and the profiler output?
It will also make a difference to plotting speed if you’re having to redraw axes every time you add a point, but it can be much faster if you have predrawn axes slightly bigger than your plot area.
No, I’m not calling drawnow(). I’ll have to look that up, I’m not familiar with it. Is it good or bad for performance?
I am redrawing axes every time through the loop. I’m not sure if I can change that or not. This is because I am also plotting text (and a couple of small images) that moves along with the new data points, showing other values related to that point. So it all has to be redrawn every time the loop parameter advances. I could look into "erasing’ the previous stuff before drawing new stuff but I’m not sure it’s worth the effort - i.e., I’d probably rather just live with the degraded performance over rewriting stuff that works well but slowly.
I don’t mind posting code but it’s currently around 1500 lines in around 15 files or so.
Drawnow is useful for basic animations, or to show intermediate results in plotting, but it will definitely slow down your plotting by 10X or more.
If you have an estimate of your axis limits, try setting the axis limits to slightly bigger than that before you start plotting. That can speed it up by 2X typically.
Think of axis limits as analogous to the size of a matrix. If you’re growing the matrix row by row, the memory reallocation will get in the way of performance. But if you can preallocate it will speed it up a lot. The same with axis limits. If you make the axes a bit bigger than you expect to use, then the plotting of each element won’t trigger an axis redraw, and the whole thing will proceed much more quickly. At the end you can always call
axis tight etc.
Added this post here, because axis-related plot slowdowns can be frequent: Technique Exchange: Speed up plotting by presetting the axis limits
Just tried this and it cut about 400 seconds. So, like 25% or so.
And since, in my case, the axis limits are known before the animation loop it’s just one line. So this is a great suggestion.