8

I have this 2 examples:

1 Example:

    Series seria = new Series("name");
    for(int i = 0 ; i < 100000 ; i++)
    {
        seria.Points.Add(new DataPoint(i, i));
    }

    seria.Points.Clear(); // - this line executes 7.10 seconds !!!!!!!!!!

Series is class from System.Windows.Forms.DataVisualization dll

2 Example:

    List<DataPoint> points = new List<DataPoint>();
    for (int i = 0; i < 100000; i++)
    {
        points.Add(new DataPoint(i, i));
    }

    points.Clear();   // - this line executes 0.0001441 seconds !!!!!!!!!!
  • Why there is so huge difference between those Clear methods?
  • And how can I clear seria.Point faster ?
braX
  • 11,506
  • 5
  • 20
  • 33
Zlobaton
  • 664
  • 1
  • 9
  • 21

1 Answers1

9

This is a very well known problem: performance problem in MSChart DataPointCollection.Clear()

Suggested workaround is like below:

public void ClearPointsQuick()
{
    Points.SuspendUpdates();
    while (Points.Count > 0)
        Points.RemoveAt(Points.Count - 1);
    Points.ResumeUpdates();
}

Inherently, while clearing points the data visualizer should suspend the updates already, but it doesn't! So above workaround will will work about a million times faster than simply calling Points.Clear() (of course until the actual bug is fixed).

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
  • why would you do `while (Points.Count > 0) ...` instead of just calling `Points.Clear()`? You already suspended updates, so that wouldn't be an issue. – Andrey Apr 21 '11 at 13:55
  • 4
    I don't know the implementation details of Clear(), whether if has an incorrect call to ResumeUpdates() or another function that interacts with the layout. So it is best to steer clear of Points.Clear() until it is fixed (hence the _workaround_). – Teoman Soygul Apr 21 '11 at 14:01
  • After testing this, it does seem that Points.Clear() does /something/ and that the above works much better – fbstj Sep 23 '15 at 13:07
  • That link to Connect is dead, and there seems to be no direct replacement. The OP's question describes the problem adequately. I suppose the page that was at the other end of that link may have explained why the problem exists, but who cares as long as this solution works. It'd be nice to know why, though. – DarenW Nov 14 '18 at 19:35