Search code examples
c#collectionsmschart

DataPointCollection Clear performance


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 ?

Solution

  • 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).