如果在ToLookup之前放一个额外的ToArray会更快吗?

Lei*_*ang 10 c# linq

我们有一个简短的方法将.csv文件解析为查找:

ILookup<string, DgvItems> ParseCsv( string fileName )
{
    var file = File.ReadAllLines( fileName );
    return file.Skip( 1 ).Select( line => new DgvItems( line ) ).ToLookup( item => item.StocksID );
}
Run Code Online (Sandbox Code Playgroud)

以及DgvItems的定义:

public class DgvItems
{
    public string DealDate { get; }

    public string StocksID { get; }

    public string StockName { get; }

    public string SecBrokerID { get; }

    public string SecBrokerName { get; }

    public double Price { get; }

    public int BuyQty { get; }

    public int CellQty { get; }

    public DgvItems( string line )
    {
        var split = line.Split( ',' );
        DealDate = split[0];
        StocksID = split[1];
        StockName = split[2];
        SecBrokerID = split[3];
        SecBrokerName = split[4];
        Price = double.Parse( split[5] );
        BuyQty = int.Parse( split[6] );
        CellQty = int.Parse( split[7] );
    }
}
Run Code Online (Sandbox Code Playgroud)

而且我们发现,如果我们增加一个额外的ToArray()之前ToLookup()是这样的:

static ILookup<string, DgvItems> ParseCsv( string fileName )
{
    var file = File.ReadAllLines( fileName  );
    return file.Skip( 1 ).Select( line => new DgvItems( line ) ).ToArray().ToLookup( item => item.StocksID );
}
Run Code Online (Sandbox Code Playgroud)

后者明显更快。更具体地说,当使用具有140万行的测试文件时,前者大约需要4.3秒,而后者大约需要3秒。

我希望ToArray()应该花更多的时间,以便后者会稍微慢一些。为什么实际上更快?


额外的信息:

  1. 我们发现此问题的原因是,还有另一种方法可以将同一.csv文件解析为不同的格式,并且大约需要3秒钟,因此我们认为该方法应该能够在3秒钟内完成相同的操作。

  2. 原始数据类型为Dictionary<string, List<DgvItems>>,原始代码未使用linq,结果相似。


BenchmarkDotNet测试类别:

public class TestClass
{
    private readonly string[] Lines;

    public TestClass()
    {
        Lines = File.ReadAllLines( @"D:\20110315_Random.csv" );
    }

    [Benchmark]
    public ILookup<string, DgvItems> First()
    {
        return Lines.Skip( 1 ).Select( line => new DgvItems( line ) ).ToArray().ToLookup( item => item.StocksID );
    }

    [Benchmark]
    public ILookup<string, DgvItems> Second()
    {
        return Lines.Skip( 1 ).Select( line => new DgvItems( line ) ).ToLookup( item => item.StocksID );
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

| Method |    Mean |    Error |   StdDev |
|------- |--------:|---------:|---------:|
|  First | 2.530 s | 0.0190 s | 0.0178 s |
| Second | 3.620 s | 0.0217 s | 0.0203 s |
Run Code Online (Sandbox Code Playgroud)

我根据原始代码进行了另一个测试。看来问题不在Linq上。

public class TestClass
{
    private readonly string[] Lines;

    public TestClass()
    {
        Lines = File.ReadAllLines( @"D:\20110315_Random.csv" );
    }

    [Benchmark]
    public Dictionary<string, List<DgvItems>> First()
    {
        List<DgvItems> itemList = new List<DgvItems>();
        for ( int i = 1; i < Lines.Length; i++ )
        {
            itemList.Add( new DgvItems( Lines[i] ) );
        }

        Dictionary<string, List<DgvItems>> dictionary = new Dictionary<string, List<DgvItems>>();

        foreach( var item in itemList )
        {
            if( dictionary.TryGetValue( item.StocksID, out var list ) )
            {
                list.Add( item );
            }
            else
            {
                dictionary.Add( item.StocksID, new List<DgvItems>() { item } );
            }
        }

        return dictionary;
    }

    [Benchmark]
    public Dictionary<string, List<DgvItems>> Second()
    {
        Dictionary<string, List<DgvItems>> dictionary = new Dictionary<string, List<DgvItems>>();
        for ( int i = 1; i < Lines.Length; i++ )
        {
            var item = new DgvItems( Lines[i] );

            if ( dictionary.TryGetValue( item.StocksID, out var list ) )
            {
                list.Add( item );
            }
            else
            {
                dictionary.Add( item.StocksID, new List<DgvItems>() { item } );
            }
        }

        return dictionary;
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

| Method |    Mean |    Error |   StdDev |
|------- |--------:|---------:|---------:|
|  First | 2.470 s | 0.0218 s | 0.0182 s |
| Second | 3.481 s | 0.0260 s | 0.0231 s |
Run Code Online (Sandbox Code Playgroud)

The*_*ias 2

我设法用下面的简化代码复制了这个问题:

var lookup = Enumerable.Range(0, 2_000_000)
    .Select(i => ( (i % 1000).ToString(), i.ToString() ))
    .ToArray() // +20% speed boost
    .ToLookup(x => x.Item1);
Run Code Online (Sandbox Code Playgroud)

重要的是所创建的元组的成员是字符串。从上面的代码中删除这两个.ToString()就消除了ToArray. .NET Framework 的行为与 .NET Core 略有不同,因为只需删除第一个就足以.ToString()消除观察到的差异。

我不知道为什么会发生这种情况。