繁体   English   中英

搜索大型阵列时性能降低

[英]Slow performance when searching large arrays

在我当前正在构建的程序中,为组成部分的每个跟踪存储了一系列数组。 每条迹线都由多个双精度数组组成,所有数组的长度均为4096。最多可以获取4条迹线。

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

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

在后台,每次触发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条或更多条迹线处于活动状态,它的速度就会大大降低。

在不增加间隔的情况下,如何加快速度?

经过一些搜索后,我发现了为什么MouseMove的更新速度如此之慢。

我当时的假设是性能下降与某种程度上是通过数组搜索有关的,但是多亏了克里斯的评论,我改而研究了绘图方面,发现我本应使用FastLine时不正确地使用Line的SeriesChartType 。

从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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM