![]() ![]() But when performance is important, we can still display a legend without its usual performance cost. Sometimes we actually wish for this dynamic effect (last year I explained how to use the legend’s undocumented -DynamicLegend feature for even greater dynamic control). Of course, it prevents the legend from being dynamically updated. ![]() Now this is much much better – a 76% performance boost compared to the original plot (i.e., 4 times faster!). > setappdata(gca,'LegendColorbarReclaimSpace',1) > setappdata(gca,'LegendColorbarManualSpace',1) > setappdata ( gca, 'LegendColorbarReclaimSpace', 1 ) > setappdata ( gca, 'LegendColorbarManualSpace', 1 ) > set ( gca, 'LegendColorbarListeners', ) ![]() Looking inside the legend code (specifically, the legendcolorbarlayout function), we detect several short-circuits that we can use to make the legend static and prevent recomputation: Running the profiler on the code above we see that much of the time is spent recomputing the legend. Hack #1 gave us a 22% performance boost, but we can do much better. But if performance is important, we now know that we have this tool to improve it. Of course, setting manual limits prevents the axes limits from growing and shrinking automatically with the data, which can actually be a very useful feature sometimes. Note that this trick is the basis for the performance improvement that occurs when using the plot’s undocumented set of LimInclude properties. > for idx = 1 : length(x) plot(x(idx),y(idx)) drawnow end > for idx = 1 : length (x ) plot (x (idx ),y (idx ) ) drawnow end > plot (x ( 1 ),y ( 1 ) ) hold on legend data Let us simulate the situation by adding 500 data points to a plot, one at a time: If instead we manually set the axes limits to the requested range, Matlab skips these checks, enabling much faster plotting performance. ![]() This computation-intensive task is done for any limit that is set to ‘Auto’ mode, which is the default axes limits mode. Whenever Matlab updates plot data, it checks whether any modification needs to be done to any of its limits. Even in such cases, we can still find important performance tricks, such as these: Performance hack #1: manual limits Unfortunately, sometimes (as in my specific case above) all these cannot help. I begin by stating the obvious: whenever possible, try to vectorize your code, preallocate the data and other performance-improving techniques suggested by Matlab. Today, I want to share a couple of very simple undocumented hacks that significantly improve plotting performance and fixed my problem. Matlab’s standard plotting functions simply could not keep up with the rate of data change. I recently consulted to a client who wanted to display an interactive plot with numerous data points that kept updating in real-time. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |