Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Performance: scatter vs. line

October 14, 2009 4 Comments

Following my previous article on the undocumented behavior of the scatter function, 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.

>> 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 and about cellfun‘s undocumented options. If you are aware of other similar functions having identical outputs and a significant performance difference, please let me know.

Related posts:

  1. Plot performance – Undocumented inner plot mechanisms can significantly improve plotting performance ...
  2. datestr performance – Caching is a simple and very effective means to improve code performance, as demonstrated for the datestr function....
  3. Undocumented scatter plot behavior – The scatter plot function has an undocumented behavior when plotting more than 100 points: it returns a single unified patch object handle, rather than a patch handle for each specific point as it returns with 100 or less points....
  4. Undocumented scatter plot jitter – Matlab's scatter plot can automatically jitter data to enable better visualization of distribution density. ...
  5. Plot line transparency and color gradient – Static and interpolated (gradient) colors and transparency can be set for plot lines in HG2. ...
  6. Performance: accessing handle properties – Handle object property access (get/set) performance can be significantly improved using dot-notation. ...
Handle graphics Performance Pure Matlab
Print Print
« Previous
Next »
4 Responses
  1. Naor October 14, 2009 at 13:05 Reply

    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. Benoit October 15, 2009 at 02:54 Reply

    In this case, lines are faster than scatter only when colors are used and different for each point (???). Observe this script:

    %% Scatter creation
    T = 0;
    cla
    for i=1:10
    	x = rand(1,1000);
    	y = rand(1,1000);
    	cla
    	t = cputime;
    	h = scatter( x,y );
    	drawnow
    	T = T + (cputime-t);
    end
    fprintf( 'Scatter creations: %.2fn', T )
     
    %% Lines creation
    T = 0;
    cla
    for i=1:10
    	x = rand(1,1000);
    	y = rand(1,1000);
    	cla
    	t = cputime;
    	h = line( [x;x], [y;y], props{:} );
    	drawnow
    	T = T + (cputime-t);
    end
    fprintf( 'Line creations: %.2fn', T )
     
    %% Scatter modification
    T = 0;
    cla
    x = rand(1,1000);
    y = rand(1,1000);
    h = scatter(x,y);
    for i=1:10
    	y = rand(1,1000);
    	t = cputime;
    	set( h, 'YData', y );
    	drawnow
    	T = T + (cputime-t);
    end
    fprintf( 'nScatter modifications: %.2fn', T )
     
    %% Lines modification
    T = 0;
    cla
    x = rand(1,1000);
    y = rand(1,1000);
    h = line( [x;x], [y;y], props{:} );
    for i=1:10
    	y = rand(1,1000);
    	t = cputime;
    	set( h, {'YData'}, num2cell([y;y]',2) );
    	drawnow
    	T = T + (cputime-t);
    end
    fprintf( 'Line modifications: %.2fn', T )
     
    %% Scatter color creation
    T = 0;
    cla
    for i=1:10
    	x = rand(1,1000);
    	y = rand(1,1000);
    	c = rand(1000,3);
    	cla
    	t = cputime;
    	h = scatter( x,y,50,c );
    	drawnow
    	T = T + (cputime-t);
    end
    fprintf( 'nScatter color creation: %.2fn', T )
     
    %% Lines color creation
    T = 0;
    cla
    for i=1:10
    	x = rand(1,1000);
    	y = rand(1,1000);
    	c = rand(1000,3);
    	cla
    	t = cputime;
    	h = line( [x;x], [y;y], props{:} );
    	set( h, {'MarkerEdgeColor'}, num2cell(c,2) );
    	drawnow
    	T = T + (cputime-t);
    end
    fprintf( 'Line color creation: %.2fn', T )
     
    %% Scatter color modification
    T = 0;
    cla
    x = rand(1,1000);
    y = rand(1,1000);
    c = rand(1000,3);
    h = scatter(x,y,50,c);
    for i=1:10
    	y = rand(1,1000);
    	t = cputime;
    	set( h, 'YData', y );
    	drawnow
    	T = T + (cputime-t);
    end
    fprintf( 'nScatter color modification: %.2fn', T )
     
    %% Lines modification
    T = 0;
    cla
    x = rand(1,1000);
    y = rand(1,1000);
    c = rand(1000,3);
    h = line( [x;x], [y;y], props{:} );
    set( h, {'MarkerEdgeColor'}, num2cell(c,2) );
    for i=1:10
    	y = rand(1,1000);
    	t = cputime;
    	set( h, {'YData'}, num2cell([y;y]',2) );
    	drawnow
    	T = T + (cputime-t);
    end
    fprintf( 'Line color modification: %.2fn', T )

    %% Scatter creation T = 0; cla for i=1:10 x = rand(1,1000); y = rand(1,1000); cla t = cputime; h = scatter( x,y ); drawnow T = T + (cputime-t); end fprintf( 'Scatter creations: %.2fn', T ) %% Lines creation T = 0; cla for i=1:10 x = rand(1,1000); y = rand(1,1000); cla t = cputime; h = line( [x;x], [y;y], props{:} ); drawnow T = T + (cputime-t); end fprintf( 'Line creations: %.2fn', T ) %% Scatter modification T = 0; cla x = rand(1,1000); y = rand(1,1000); h = scatter(x,y); for i=1:10 y = rand(1,1000); t = cputime; set( h, 'YData', y ); drawnow T = T + (cputime-t); end fprintf( 'nScatter modifications: %.2fn', T ) %% Lines modification T = 0; cla x = rand(1,1000); y = rand(1,1000); h = line( [x;x], [y;y], props{:} ); for i=1:10 y = rand(1,1000); t = cputime; set( h, {'YData'}, num2cell([y;y]',2) ); drawnow T = T + (cputime-t); end fprintf( 'Line modifications: %.2fn', T ) %% Scatter color creation T = 0; cla for i=1:10 x = rand(1,1000); y = rand(1,1000); c = rand(1000,3); cla t = cputime; h = scatter( x,y,50,c ); drawnow T = T + (cputime-t); end fprintf( 'nScatter color creation: %.2fn', T ) %% Lines color creation T = 0; cla for i=1:10 x = rand(1,1000); y = rand(1,1000); c = rand(1000,3); cla t = cputime; h = line( [x;x], [y;y], props{:} ); set( h, {'MarkerEdgeColor'}, num2cell(c,2) ); drawnow T = T + (cputime-t); end fprintf( 'Line color creation: %.2fn', T ) %% Scatter color modification T = 0; cla x = rand(1,1000); y = rand(1,1000); c = rand(1000,3); h = scatter(x,y,50,c); for i=1:10 y = rand(1,1000); t = cputime; set( h, 'YData', y ); drawnow T = T + (cputime-t); end fprintf( 'nScatter color modification: %.2fn', T ) %% Lines modification T = 0; cla x = rand(1,1000); y = rand(1,1000); c = rand(1000,3); h = line( [x;x], [y;y], props{:} ); set( h, {'MarkerEdgeColor'}, num2cell(c,2) ); for i=1:10 y = rand(1,1000); t = cputime; set( h, {'YData'}, num2cell([y;y]',2) ); drawnow T = T + (cputime-t); end fprintf( 'Line color modification: %.2fn', T )

    When you execute this script, you obtain:

    Scatter creations: 0.76
    Line creations: 9.53
     
    Scatter modifications: 0.61
    Line modifications: 1.51
     
    Scatter color creation: 4.02
    Line color creation: 17.07
     
    Scatter color modification: 4.34
    Line color modification: 1.53

    Scatter creations: 0.76 Line creations: 9.53 Scatter modifications: 0.61 Line modifications: 1.51 Scatter color creation: 4.02 Line color creation: 17.07 Scatter color modification: 4.34 Line color modification: 1.53

    Magic, isn’t it?
    Sometimes, I wonder if Matlab was not developed at Hogwarts…

  3. Naor October 15, 2009 at 15:41 Reply

    very strange indeed.

  4. damayi May 22, 2012 at 19:59 Reply

    In my application, I will use line as much as possible.
    What’s more, line is more simple and easy to control.

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
ActiveX (6) AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
  • Nicholas (6 days 15 hours ago): Hi Yair, Thanks for the reply. I am on Windows 10. I also forgot to mention that this all works wonderfully out of the editor. It only fails once compiled. So, yes, I have tried a...
  • Nicholas (6 days 15 hours ago): Hi Yair, Thanks for the reply. I am on Windows 10. I also forgot to mention that this all works wonderfully out of the editor. It only fails once compiled. So, yes, I have tried a...
  • Yair Altman (6 days 22 hours ago): Nicholas – yes, I used it in a compiled Windows app using R2022b (no update). You didn’t specify the Matlab code location that threw the error so I can’t help...
  • Nicholas (7 days 18 hours ago): Hi Yair, Have you attempted your displayWebPage utility (or the LightweightHelpPanel in general) within a compiled application? It appears to fail in apps derived from both R2022b...
  • João Neves (10 days 23 hours ago): I am on matlab 2021a, this still works: url = struct(struct(struct(struct(hF ig).Controller).PlatformHost). CEF).URL; but the html document is empty. Is there still a way to do...
  • Yair Altman (13 days 22 hours ago): Perhaps the class() function could assist you. Or maybe just wrap different access methods in a try-catch so that if one method fails you could access the data using another...
  • Jeroen Boschma (14 days 0 hours ago): Never mind, the new UI components have an HTML panel available. Works for me…
  • Alexandre (14 days 1 hour ago): Hi, Is there a way to test if data dictionnatry entry are signal, simulink parameters, variables … I need to access their value, but the access method depends on the data...
  • Nicholas (14 days 16 hours ago): In case anyone is looking for more info on the toolbar: I ran into some problems creating a toolbar with the lightweight panel. Previously, the Browser Panel had an addToolbar...
  • Jeroen Boschma (17 days 23 hours ago): I do not seem to get the scrollbars (horizontal…) working in Matlab 2020b. Snippets of init-code (all based on Yair’s snippets on this site) handles.text_explorer...
  • Yair Altman (46 days 1 hour ago): m_map is a mapping tool, not even created by MathWorks and not part of the basic Matlab system. I have no idea why you think that the customizations to the builtin bar function...
  • chengji chen (46 days 7 hours ago): Hi, I have tried the method, but it didn’t work. I plot figure by m_map toolbox, the xticklabel will add to the yticklabel at the left-down corner, so I want to move down...
  • Yair Altman (54 days 0 hours ago): @Alexander – this is correct. Matlab stopped including sqlite4java in R2021b (it was still included in 21a). You can download the open-source sqlite4java project from...
  • Alexander Eder (59 days 20 hours ago): Unfortunately Matlab stopped shipping sqlite4java starting with R2021(b?)
  • K (66 days 7 hours ago): Is there a way to programmatically manage which figure gets placed where? Let’s say I have 5 figures docked, and I split it into 2 x 1, I want to place 3 specific figures on the...
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top