title: Drawing zig-zag lines is much slower than drawing straight lines
body: <p>While using a self-written graphing control I noticed that the painting of the graph was much slower while displaying noisy data than when it displayed clean data.<br>
  I dug further into and narrowed the problem down to its bare minimum difference: Drawing the same amount of lines with varying Y values versus drawing lines with the same Y value.</p>
  
  <p>So for example I put together the following tests. I generate lists of points, one with random Y values, one with the same Y, and one with a Zig-Zag Y pattern.</p>
  
  <pre><code>private List&lt;PointF&gt; GenerateRandom(int n, int width, int height)
  {
      //Generate random pattern
      Random rnd = new Random();
      float stepwidth = Convert.ToSingle(width / n);
      float mid = Convert.ToSingle(height / 2);
      float lastx = 0;
      float lasty = mid;
      List&lt;PointF&gt; res = new List&lt;PointF&gt;();
      res.Add(new PointF(lastx, lasty));
      for (int i = 1; i &lt;= n; i++)
      {
          var x = stepwidth * i;
          var y = Convert.ToSingle(height * rnd.NextDouble());
          res.Add(new PointF(x, y));
      }
      return res;
  }
  private List&lt;PointF&gt; GenerateUnity(int n, int width, int height)
  {
      //Generate points along a simple line
      float stepwidth = Convert.ToSingle(width / n);
      float mid = Convert.ToSingle(height / 2);
      float lastx = 0;
      float lasty = mid;
      List&lt;PointF&gt; res = new List&lt;PointF&gt;();
      res.Add(new PointF(lastx, lasty));
      for (int i = 1; i &lt;= n; i++)
      {
          var x = stepwidth * i;
          var y = mid;
          res.Add(new PointF(x, y));
      }
      return res;
  }
  private List&lt;PointF&gt; GenerateZigZag(int n, int width, int height)
  {
      //Generate an Up/Down List
      float stepwidth = Convert.ToSingle(width / n);
      float mid = Convert.ToSingle(height / 2);
      float lastx = 0;
      float lasty = mid;
      List&lt;PointF&gt; res = new List&lt;PointF&gt;();
      res.Add(new PointF(lastx, lasty));
      var state = false;
      for (int i = 1; i &lt;= n; i++)
      {
          var x = stepwidth * i;
          var y = mid - (state ? 50 : -50);
          res.Add(new PointF(x, y));
          state = !state;
      }
      return res;
  }
  </code></pre>
  
  <p>I now draw each list of points a few times and compare how long it takes:</p>
  
  <pre><code>private void DoTheTest()
  {
      Bitmap bmp = new Bitmap(970, 512);
      var random = GenerateRandom(2500, bmp.Width, bmp.Height).ToArray();
      var unity = GenerateUnity(2500, bmp.Width, bmp.Height).ToArray();
      var ZigZag = GenerateZigZag(2500, bmp.Width, bmp.Height).ToArray();
  
      using (Graphics g = Graphics.FromImage(bmp))
      {
          var tUnity = BenchmarkDraw(g, 200, unity);
          var tRandom = BenchmarkDraw(g, 200, random);
          var tZigZag = BenchmarkDraw(g, 200, ZigZag);
          MessageBox.Show(tUnity.ToString() + "\r\n" + tRandom.ToString() + "\r\n" + tZigZag.ToString());
      }
  }
  private double BenchmarkDraw(Graphics g, int n, PointF[] Points)
  {
      var Times = new List&lt;double&gt;();
      for (int i = 1; i &lt;= n; i++)
      {
          g.Clear(Color.White);
          System.DateTime d3 = DateTime.Now;
          DrawLines(g, Points);
          System.DateTime d4 = DateTime.Now;
          Times.Add((d4 - d3).TotalMilliseconds);
      }
      return Times.Average();
  }
  private void DrawLines(Graphics g, PointF[] Points)
  {
      g.DrawLines(Pens.Black, Points);
  }
  </code></pre>
  
  <p>I come up with the following durations per draw:</p>
  
  <pre><code>Straight Line: 0.095 ms
  Zig-Zag Pattern: 3.24 ms
  Random Pattern: 5.47 ms
  </code></pre>
  
  <p>So it seems to get progressively worse, the more change there is in the lines to be drawn, and that is also a real world effect I encountered in the control painting I mentioned in the beginning.</p>
  
  <p>My questions are thus the following:</p>
  
  <ol>
  <li>Why does it make a such a brutal difference, which lines are to be drawn?</li>
  <li>How can I improve the drawing speed for the noisy data?</li>
  </ol>