CsvHelper在VisualStudio 2013中非常慢

时间:2018-06-07 07:54:13

标签: c# visual-studio-2015 visual-studio-2013 csvhelper

我在Visual Studio 2015中使用CsvHelper工作。它工作正常。然后我切换到Visual Studio 2013进行一些测试,CsvHelper工作得很慢。在VS 2015中最多花费2秒钟来读取VS2013中的大文件需要5分钟以上。

我测试在VS2013和VS2015中打开的同一个项目。在VS2013中运行缓慢,在VS2015中则相反。同一个项目。

因此,VS2013必须做一些事情才能导致CsvHelper速度变慢。有什么想法吗?

编辑: 为了澄清,我在调试模式下运行每个测试。

我添加了我的实际阅读功能:

    internal void ReadInCSVPoint3DNew(string absolutePath)
    {
        CultureInfo Culture = new CultureInfo("en-US");
        List<Point3D> result = new List<Point3D>();
        string value;
        using (TextReader fileReader = File.OpenText(absolutePath))
        {
            var csv = new CsvReader(fileReader);
            csv.Configuration.HasHeaderRecord = false; // The head is not in good format, so, setting it true doesn't work.
            csv.Read(); // Skip Head
            while (csv.Read())
            {
                string[] Strings = new String[13];
                for (int i = 0; csv.TryGetField<string>(i, out value); i++)
                {
                    Strings[i] = value;
                }

                Point3D point = new Point3D()
                {
                    PointX = (decimal)float.Parse(Strings[0], Culture),
                    PointY = (decimal)float.Parse(Strings[1], Culture),
                    PointZ = (decimal)float.Parse(Strings[2], Culture),
                    X = (decimal)float.Parse(Strings[3], Culture),
                    Y = (decimal)float.Parse(Strings[4], Culture),
                    Z = (decimal)float.Parse(Strings[5], Culture),
                    Intensity = (int)float.Parse(Strings[6], Culture),
                    LaserIndex = (int)float.Parse(Strings[7], Culture),
                    Azimuth = (int)float.Parse(Strings[8], Culture),
                    Distance = (decimal)float.Parse(Strings[9], Culture),
                    AdjustTime = (long)float.Parse(Strings[10], Culture),
                    TimeStamp = (long)float.Parse(Strings[11], Culture),
                    VerticalAngle = (int)float.Parse(Strings[12], Culture)
                };
                result.Add(point);
            }
        }
        this.Data = result;
    }

1 个答案:

答案 0 :(得分:1)

问题在于代码。 Panagiotis Kanavos指出:

  

块引用   此外,代码有一些错误会影响旧垃圾收集器的性能。您正在构建一个新的空字符串数组并将其丢弃在每一行中。这是每条线上收集的1个孤儿对象。

我从代码中删除了字符串数组,现在在VS2013和VS2015中以相同的速度工作。我也改变了解析。代码现在看起来像这样:

    internal void ReadInCSVPoint3DNew(string absolutePath)
    {
        CultureInfo Culture = new CultureInfo("en-US");
        List<Point3D> result = new List<Point3D>();
        using (TextReader fileReader = File.OpenText(absolutePath))
        {
            var csv = new CsvReader(fileReader);
            csv.Configuration.HasHeaderRecord = false; // The head is not in good format, so, setting it true doesn't work.
            csv.Read(); // Skip Head
            while (csv.Read())
            {
                Point3D point = new Point3D()
                {
                    PointX = decimal.Parse(csv.GetField(0), System.Globalization.NumberStyles.Float, Culture),
                    PointY = decimal.Parse(csv.GetField(1), System.Globalization.NumberStyles.Float, Culture),
                    PointZ = decimal.Parse(csv.GetField(2), System.Globalization.NumberStyles.Float, Culture),
                    X = decimal.Parse(csv.GetField(3), System.Globalization.NumberStyles.Float, Culture),
                    Y = decimal.Parse(csv.GetField(4), System.Globalization.NumberStyles.Float, Culture),
                    Z = decimal.Parse(csv.GetField(5), System.Globalization.NumberStyles.Float, Culture),
                    Intensity = int.Parse(csv.GetField(6), System.Globalization.NumberStyles.Float, Culture),
                    LaserIndex = int.Parse(csv.GetField(7), System.Globalization.NumberStyles.Float, Culture),
                    Azimuth = int.Parse(csv.GetField(8), System.Globalization.NumberStyles.Float, Culture),
                    Distance = decimal.Parse(csv.GetField(9), System.Globalization.NumberStyles.Float, Culture),
                    AdjustTime = long.Parse(csv.GetField(10), System.Globalization.NumberStyles.Float, Culture),
                    TimeStamp = long.Parse(csv.GetField(11), System.Globalization.NumberStyles.Float, Culture),
                    VerticalAngle = int.Parse(csv.GetField(12), System.Globalization.NumberStyles.Float, Culture)
                };
                result.Add(point);
            }
        }
        this.Data = result;
    }

感谢Panagiotis Kanavos的大力帮助。