Performance: scatter vs. line

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.

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.

Categories: Handle graphics, Low risk of breaking in future versions, Memory, Stock Matlab function

Tags: , ,

Bookmark and SharePrint Print

4 Responses to Performance: scatter vs. line

  1. Naor says:

    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 says:

    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: %.2f\n', 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: %.2f\n', 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: %.2f\n', 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: %.2f\n', 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: %.2f\n', 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: %.2f\n', 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: %.2f\n', 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: %.2f\n', 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

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

  3. Naor says:

    very strange indeed.

  4. damayi says:

    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


Your email address will not be published. Required fields are marked *