- Undocumented Matlab - https://undocumentedmatlab.com -
Performance: scatter vs. line
Posted By Yair Altman On October 14, 2009 | 4 Comments
Following my previous article on the undocumented behavior of the scatter function [1], one of my readers, Benoit Charles, reported a discovery that in many circumstances the line function generates identical plots much faster than scatter.
Unlike scatter, line does not enable specific data-point marker customization, although the colors could be modified. On the other hand, line only uses a single handle object, saving memory and system resources compared to scatter keeping a separate handle for each data point. So, if you just need to quickly plot a bunch of scattered points then line could be a better choice than scatter.
Here is a simple code snippet, which generates identical plots and shows the performance difference:
>> x=rand(1000,1); y=rand(1000,1);
>> tic, for idx=1:100, cla; h=scatter(x,y); end; toc
Elapsed time is 2.521322 seconds.
>> props = {'LineStyle','none','Marker','o','MarkerEdge','b','MarkerSize',6};
>> tic, for idx=1:100, cla; h=line([x,x],[y,y],props{:}); end; toc
Elapsed time is 0.333369 seconds.
In the past, I have posted about other undocumented performance aspects, comparing the documented ismember function with the undocumented ismembc [2] and about cellfun‘s undocumented options [3]. If you are aware of other similar functions having identical outputs and a significant performance difference, please let me know.
Categories: Handle graphics, Low risk of breaking in future versions, Memory, Stock Matlab function
Article printed from Undocumented Matlab: https://undocumentedmatlab.com
URL to article: https://undocumentedmatlab.com/articles/performance-scatter-vs-line
URLs in this post:
[1] undocumented behavior of the scatter function: http://undocumentedmatlab.com/blog/undocumented-scatter-plot-behavior/
[2] comparing the documented ismember function with the undocumented ismembc: http://undocumentedmatlab.com/blog/ismembc-undocumented-helper-function/
[3] cellfun‘s undocumented options: http://undocumentedmatlab.com/blog/cellfun-undocumented-performance-boost/
[4] Plot performance : https://undocumentedmatlab.com/articles/plot-performance
[5] datestr performance : https://undocumentedmatlab.com/articles/datestr-performance
[6] Undocumented scatter plot behavior : https://undocumentedmatlab.com/articles/undocumented-scatter-plot-behavior
[7] Undocumented scatter plot jitter : https://undocumentedmatlab.com/articles/undocumented-scatter-plot-jitter
[8] Plot line transparency and color gradient : https://undocumentedmatlab.com/articles/plot-line-transparency-and-color-gradient
[9] Performance: accessing handle properties : https://undocumentedmatlab.com/articles/performance-accessing-handle-properties
Click here to print.
Copyright © Yair Altman - Undocumented Matlab. All rights reserved.
4 Comments To "Performance: scatter vs. line"
#1 Comment By Naor On October 14, 2009 @ 13:05
Now this is really interesting. Now I’m curious about generating 1000 line objects with one data point/marker each, and saving into a handle vector? Is this what scatter is doing under the hood?
#2 Comment By Benoit On October 15, 2009 @ 02:54
In this case, lines are faster than scatter only when colors are used and different for each point (???). Observe this script:
When you execute this script, you obtain:
Magic, isn’t it?
Sometimes, I wonder if Matlab was not developed at Hogwarts…
#3 Comment By Naor On October 15, 2009 @ 15:41
very strange indeed.
#4 Comment By damayi On May 22, 2012 @ 19:59
In my application, I will use line as much as possible.
What’s more, line is more simple and easy to control.