title: MATLAB scatter3, plot3 speed discrepencies
body: <p>This is about how MATLAB can take very different times to plot the same thing — and why.</p>
  
  <p>I generate 10000 points in 3D space:</p>
  
  <pre><code>X = rand(10000, 1);
  Y = rand(10000, 1);
  Z = rand(10000, 1);
  </code></pre>
  
  <p>I then used one of four different methods to plot this, to create a plot like so:</p>
  
  <p><img src="https://i.stack.imgur.com/wrkqr.png" alt="alt text"></p>
  
  <p>I closed all figures and cleared the workspace between each run to try to ensure fairness.</p>
  
  <p><strong>Bulk plotting using scatter3:</strong></p>
  
  <pre><code>&gt;&gt; tic; scatter3(X, Y, Z); drawnow; toc
  Elapsed time is 0.815450 seconds.
  </code></pre>
  
  <p><strong>Individual plotting using scatter3:</strong></p>
  
  <pre><code>&gt;&gt; tic; hold on;
  for i = 1:10000
      scatter3(X(i), Y(i), Z(i), 'b');
  end
  hold off; drawnow; toc
  Elapsed time is 51.469547 seconds.
  </code></pre>
  
  <p><strong>Bulk plotting using plot3:</strong></p>
  
  <pre><code>&gt;&gt; tic; plot3(X, Y, Z, 'o'); drawnow; toc
  Elapsed time is 0.153480 seconds.
  </code></pre>
  
  <p><strong>Individual plotting using plot3:</strong></p>
  
  <pre><code>&gt;&gt; tic; hold on
  for i = 1:10000
      plot3(X(i), Y(i), Z(i), 'o');
  end
  drawnow; toc
  Elapsed time is 5.854662 seconds.
  </code></pre>
  
  <p>What is it that MATLAB does behind the scenes in the 'longer' routines to take so long? What are the advantages and disadvantages of using each method?</p>
  
  <p><strong>Edit:</strong>
  Thanks to advice from Ben Voigt (see answers), I have included <code>drawnow</code> commands in the timing — but this has made little difference to the times.</p>