关于收集的Foreach转换为IEnumerable工作比没有转换时慢?

Grz*_*cki 8 c# linq

今天我找到了一些我不太了解的东西.我在LinqPad(版本5)中获得了以下代码:

void Main()
{
    const int size = 5000000;
    List<Thing> things = Enumerable.Range(1, 5000000).Select(x => new Thing {Id = x}).ToList();

    var sw1 = Stopwatch.StartNew();
    foreach (var t in things)
        if(t.Id == size) break;
    sw1.ElapsedMilliseconds.Dump();

    var sw2 = Stopwatch.StartNew();
    IEnumerable<Thing> ienThings = things;
    foreach (var t in ienThings)
        if (t.Id == size) break;
    sw2.ElapsedMilliseconds.Dump();

}

class Thing
{
    public long Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

看起来第二个循环的时间是第一个循环的两倍.为什么这个简单的演员会产生这样的效果呢?我确信在幕后发生了一些简单的事情,我不知何故.

Geo*_*voy 7

这是由于使用的callcallvirt指令之间的差异.

call        System.Collections.Generic.List<UserQuery+Thing>+Enumerator.get_Current
call        System.Collections.Generic.List<UserQuery+Thing>+Enumerator.MoveNext
Run Code Online (Sandbox Code Playgroud)

VS

callvirt    System.Collections.Generic.IEnumerator<UserQuery+Thing>.get_Current
callvirt    System.Collections.IEnumerator.MoveNext
Run Code Online (Sandbox Code Playgroud)

callvirt指令执行空检查,这就是它较慢的原因.