搜索大型数组时性能下降

时间:2014-07-01 20:56:15

标签: c# arrays search charts

在我正在构建的程序中,为每个部分的跟踪存储一系列数组。每条迹线由多个双精度数组组成,长度均为4096.最多可以采集4条迹线。

此问题的相关数组是Angle数组,它是在4096个数据点(每个索引约为0.044度)内插-90到90度的值数组。此数组在C#图表控件上绘制,与另一个厚度数据数组(也是4096点)相对应。

我的图表控件还实现了MouseMove事件,该事件在图表上绘制附加到X轴(-90到90度)的光标,间隔为.1。这个想法是当用户将鼠标悬停在图表上时,会在图表顶部弹出一个注释,显示整个角度的厚度数据。

在后台,每次激活MouseMove事件时,光标在图表上的X位置被放入一个函数中,以找到跟踪1的Angle数组中最近角度的索引。这里'搜索代码:

private int FindIndexOfAngle(double Angle)
{

    int closestX = Array.BinarySearch(Traces[0].AngleArray, Angle);
    if (closestX < 0)
    {
        closestX = ~closestX; // If closestX is bitwise complement of index of next highest value from binary search

        double closestY = Traces[0].AngleArray[closestX] - Angle;
        if (Traces[0].AngleArray[closestX - 1] - Angle < closestY) closestX = closestX - 1;
    }

    return closestX;

}

这是图表MouseMove事件:

private void chartMain_MouseMove(object sender, MouseEventArgs e)
{

    try
    {
        if (chartMain.Series.Count > 0)
        {
            Point mousePoint = new Point(e.X, e.Y);

            chartMain.ChartAreas[0].CursorX.SetCursorPixelPosition(mousePoint, true);
            double pX = chartMain.ChartAreas[0].CursorX.Position;

            if (pX >= -90 && pX <= 90)
            {
                chartMain.ChartAreas[0].CursorX.LineColor = Color.Red;
                //if (pX == 90) chartMain.ChartAreas[0].CursorX.Position = 89.9;
                RectangleAnnotation anno = new RectangleAnnotation();

                int index = FindIndexOfAngle(pX);

                anno.Text = pX.ToString("0.00") + " degrees";

                if (Surface == Traces.Count)
                {
                    for (int x = 0; x < Traces.Count; x++)
                    {
                        if (MeasurementSettings.PlotRawData) anno.Text += "\nTrace " + (x + 1) + ": " + Traces[x].ThicknessRaw[index].ToString("0.000 000") + " " + MainSettings.UnitsName;
                        else anno.Text += "\nTrace " + (x + 1) + ": " + Traces[x].ThicknessFiltered[index].ToString("0.000 000") + " " + MainSettings.UnitsName;
                    }
                }
                else
                {
                    if (MeasurementSettings.PlotRawData) anno.Text += "\nTrace " + (Surface + 1) + ": " + Traces[Surface].ThicknessRaw[index].ToString("0.000 000") + " " + MainSettings.UnitsName;
                    else anno.Text += "\nTrace " + (Surface + 1) + ": " + Traces[Surface].ThicknessFiltered[index].ToString("0.000 000") + " " + MainSettings.UnitsName;
                }

                anno.Font = new Font("Microsoft Sans Serif", 12);
                anno.AnchorX = 50;
                anno.AnchorY = 14;

                chartMain.Annotations.Clear();
                chartMain.Annotations.Add(anno);

            }
            else chartMain.ChartAreas[0].CursorX.LineColor = Color.Transparent;
        }
        else chartMain.ChartAreas[0].CursorX.LineColor = Color.Transparent;
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

}

此索引随后用于显示屏幕上显示的每条迹线的厚度值(一次最多4条)。

当只有1个跟踪激活时,这非常有效。但只要有2条或更多条迹线处于活动状态,它就会大幅减速。

如果不增加间隔,我怎样才能加快速度?

1 个答案:

答案 0 :(得分:2)

在做了一些搜索后,我发现为什么我的MouseMove更新速度很慢。

我假设缓慢的性能与搜索数组有某种关系,但感谢Chris的评论,我反而调查了绘图方面,发现我不正确地使用了SeriesChartType的Line,当我应该使用FastLine。

来自MSDN:

The FastLine chart type is a variation of the Line chart that significantly 
reduces the   drawing time of a series that contains a very large number of 
data points. Use this chart in situations where very large data sets are used
and rendering speed is critical.

Some charting features are omitted from the FastLine chart to improve performance. 
The features omitted include control of point level visual attributes, markers, 
data point labels, and shadows.

http://msdn.microsoft.com/en-us/library/dd489249.aspx