在VB.NET中,foreach循环比在C#中快吗?

3

我的同事说,在以前的一次面试中,他得知VB.Net中的foreach比c#中的foreach更快。他被告知这是因为两者具有不同的CLR实现。

从C ++的角度来看,我很好奇这是为什么,我被告知需要先了解CLR。在谷歌上搜索foreach和CLR并不能帮助我理解。

是否有人能够对为什么VB.Net中的foreach比c#中的foreach更快给出一个好的解释?或者我的同事被误导了吗?


1
在询问原因之前,我想看到一些证据。这听起来完全不对。 - Matt Hamilton
@Matt,我的同事只是顺便提到了它,但我的好奇心被激起了。对我来说,这也听起来不对。但是我的好奇心获胜了,冒着被投票和嘲笑的风险,我在SO上提出了问题。我在问题中写了面试官告诉他的内容(CLR实现不同)。 - MrValdez
我猜他问这个问题是想看看你的同事对明显错误信息的反应。 - John Rasch
我下面已经回答了,但是在这里想指出的是,是的,它们在技术上确实有不同的IL实现(由于C#与VB编译器的不同),但这并不会在任何一方产生任何形式的性能提升。 - ckramer
这可能是另一则已经不再适用的轶事,有点像早期版本的VB中String.Equals的实现调用了一个VB6互操作程序集。 - Jim H.
显示剩余2条评论
5个回答

11

C#和VB.Net在IL级别上没有显着的区别。在这两个版本之间会有一些额外的Nop指令,但并没有真正改变正在发生的事情。

这是方法:(用C#编写)

public void TestForEach()
    {
        List<string> items = new List<string> { "one", "two", "three" };

        foreach (string item in items)
        {
            Debug.WriteLine(item);
        }
    }

在 VB.Net 中:

Public Sub TestForEach
    Dim items As List(Of String) = New List(Of String)()
    items.Add("one")
    items.Add("two")
    items.Add("three")
    For Each item As string In items
        Debug.WriteLine(item)
    Next
End Sub

以下是C#版本的IL代码:

.method public hidebysig instance void TestForEach() cil managed
{
    .maxstack 2
    .locals init (
        [0] class [mscorlib]System.Collections.Generic.List`1<string> items,
        [1] string item,
        [2] class [mscorlib]System.Collections.Generic.List`1<string> <>g__initLocal3,
        [3] valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<string> CS$5$0000,
        [4] bool CS$4$0001)
    L_0000: nop 
    L_0001: newobj instance void [mscorlib]System.Collections.Generic.List`1<string>::.ctor()
    L_0006: stloc.2 
    L_0007: ldloc.2 
    L_0008: ldstr "one"
    L_000d: callvirt instance void [mscorlib]System.Collections.Generic.List`1<string>::Add(!0)
    L_0012: nop 
    L_0013: ldloc.2 
    L_0014: ldstr "two"
    L_0019: callvirt instance void [mscorlib]System.Collections.Generic.List`1<string>::Add(!0)
    L_001e: nop 
    L_001f: ldloc.2 
    L_0020: ldstr "three"
    L_0025: callvirt instance void [mscorlib]System.Collections.Generic.List`1<string>::Add(!0)
    L_002a: nop 
    L_002b: ldloc.2 
    L_002c: stloc.0 
    L_002d: nop 
    L_002e: ldloc.0 
    L_002f: callvirt instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> [mscorlib]System.Collections.Generic.List`1<string>::GetEnumerator()
    L_0034: stloc.3 
    L_0035: br.s L_0048
    L_0037: ldloca.s CS$5$0000
    L_0039: call instance !0 [mscorlib]System.Collections.Generic.List`1/Enumerator<string>::get_Current()
    L_003e: stloc.1 
    L_003f: nop 
    L_0040: ldloc.1 
    L_0041: call void [System]System.Diagnostics.Debug::WriteLine(string)
    L_0046: nop 
    L_0047: nop 
    L_0048: ldloca.s CS$5$0000
    L_004a: call instance bool [mscorlib]System.Collections.Generic.List`1/Enumerator<string>::MoveNext()
    L_004f: stloc.s CS$4$0001
    L_0051: ldloc.s CS$4$0001
    L_0053: brtrue.s L_0037
    L_0055: leave.s L_0066
    L_0057: ldloca.s CS$5$0000
    L_0059: constrained [mscorlib]System.Collections.Generic.List`1/Enumerator<string>
    L_005f: callvirt instance void [mscorlib]System.IDisposable::Dispose()
    L_0064: nop 
    L_0065: endfinally 
    L_0066: nop 
    L_0067: ret 
    .try L_0035 to L_0057 finally handler L_0057 to L_0066
}

以下是VB.Net版本的中间语言(IL):

.method public instance void TestForEach() cil managed
{
    .maxstack 2
    .locals init (
        [0] class [mscorlib]System.Collections.Generic.List`1<string> items,
        [1] string item,
        [2] valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<string> VB$t_struct$L0,
        [3] bool VB$CG$t_bool$S0)
    L_0000: nop 
    L_0001: newobj instance void [mscorlib]System.Collections.Generic.List`1<string>::.ctor()
    L_0006: stloc.0 
    L_0007: ldloc.0 
    L_0008: ldstr "one"
    L_000d: callvirt instance void [mscorlib]System.Collections.Generic.List`1<string>::Add(!0)
    L_0012: nop 
    L_0013: ldloc.0 
    L_0014: ldstr "two"
    L_0019: callvirt instance void [mscorlib]System.Collections.Generic.List`1<string>::Add(!0)
    L_001e: nop 
    L_001f: ldloc.0 
    L_0020: ldstr "three"
    L_0025: callvirt instance void [mscorlib]System.Collections.Generic.List`1<string>::Add(!0)
    L_002a: nop 
    L_002b: nop 
    L_002c: ldloc.0 
    L_002d: callvirt instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> [mscorlib]System.Collections.Generic.List`1<string>::GetEnumerator()
    L_0032: stloc.2 
    L_0033: br.s L_0045
    L_0035: ldloca.s VB$t_struct$L0
    L_0037: call instance !0 [mscorlib]System.Collections.Generic.List`1/Enumerator<string>::get_Current()
    L_003c: stloc.1 
    L_003d: ldloc.1 
    L_003e: call void [System]System.Diagnostics.Debug::WriteLine(string)
    L_0043: nop 
    L_0044: nop 
    L_0045: ldloca.s VB$t_struct$L0
    L_0047: call instance bool [mscorlib]System.Collections.Generic.List`1/Enumerator<string>::MoveNext()
    L_004c: stloc.3 
    L_004d: ldloc.3 
    L_004e: brtrue.s L_0035
    L_0050: nop 
    L_0051: leave.s L_0062
    L_0053: ldloca.s VB$t_struct$L0
    L_0055: constrained [mscorlib]System.Collections.Generic.List`1/Enumerator<string>
    L_005b: callvirt instance void [mscorlib]System.IDisposable::Dispose()
    L_0060: nop 
    L_0061: endfinally 
    L_0062: nop 
    L_0063: ret 
    .try L_002c to L_0053 finally handler L_0053 to L_0062
}

1
额外的nop指令表明您是在调试模式下编译的? - Guffa
那是正确的...这只是一个快速测试,我在for each循环后使用了Debug.WriteLine() :) - ckramer

6
我对这个说法有点怀疑。foreach结构针对两种语言的处理方式相同,它从托管对象中获取IEnumerator并调用MoveNext()。原始代码是使用VB.NET还是C#编写的应该没有关系,它们都编译成相同的内容。
在我的测试时间中,VB.NET和C#中相同的foreach循环在长迭代中的差距从未超过1%。
c#:
L_0048: ldloca.s CS$5$0001
L_004a: call instance !0 [mscorlib]System.Collections.Generic.List`1/Enumerator<string>::get_Current()
L_004f: stloc.3 
L_0050: nop 
L_0051: ldloc.3 
L_0052: call void [mscorlib]System.Console::WriteLine(string)
L_0057: nop 
L_0058: nop 
L_0059: ldloca.s CS$5$0001
L_005b: call instance bool [mscorlib]System.Collections.Generic.List`1/Enumerator<string>::MoveNext()
L_0060: stloc.s CS$4$0000
L_0062: ldloc.s CS$4$0000
L_0064: brtrue.s L_0048

VB.NET:

L_0043: ldloca.s VB$t_struct$L0
L_0045: call instance !0 [mscorlib]System.Collections.Generic.List`1/Enumerator<string>::get_Current()
L_004a: stloc.s item
L_004c: ldloc.s item
L_004e: call void [mscorlib]System.Console::WriteLine(string)
L_0053: nop 
L_0054: nop 
L_0055: ldloca.s VB$t_struct$L0
L_0057: call instance bool [mscorlib]System.Collections.Generic.List`1/Enumerator<string>::MoveNext()
L_005c: stloc.s VB$CG$t_bool$S0
L_005e: ldloc.s VB$CG$t_bool$S0
L_0060: brtrue.s L_0043

这正是我的同事最初的答案,但被面试官告知是错误的。我倾向于相信面试官是错的,但我对C#不够熟练,无法给出有效的意见。因此,才有了这个问题。 - MrValdez
@MrValdez,你应该让你的同事写一封措辞强硬的信给面试官,一旦你证明他错了!;-) - Matt Hamilton
@Matt :D 很好的建议!我会告诉他的。 - MrValdez

4

对于一个简单的循环遍历字符串数组,这是VB生成的IL代码:

L_0007: ldloc.0 
L_0008: stloc.3 
L_0009: ldc.i4.0 
L_000a: stloc.2 
L_000b: br.s L_0019

L_000d: ldloc.3 
L_000e: ldloc.2 
L_000f: ldelem.ref 
L_0010: stloc.1 

...

L_0015: ldloc.2 
L_0016: ldc.i4.1 
L_0017: add.ovf 
L_0018: stloc.2 

L_0019: ldloc.2 
L_001a: ldloc.3 
L_001b: ldlen 
L_001c: conv.ovf.i4 
L_001d: blt.s L_000d

以下是C#生成的IL代码:

L_0007: ldloc.0 
L_0008: stloc.2 
L_0009: ldc.i4.0 
L_000a: stloc.3 
L_000b: br.s L_0019

L_000d: ldloc.2 
L_000e: ldloc.3 
L_000f: ldelem.ref 
L_0010: stloc.1 

...

L_0015: ldloc.3 
L_0016: ldc.i4.1 
L_0017: add 
L_0018: stloc.3 

L_0019: ldloc.3 
L_001a: ldloc.2 
L_001b: ldlen 
L_001c: conv.i4 
L_001d: blt.s L_000d

唯一的区别在于VB使用add.ovfconv.ovf.i4代替addconv.i4。这意味着VB代码执行了两个额外的溢出检查,可能会稍微慢一些。

3

VB.NET和C#都使用相同的CLR。我刚刚使用了以下代码进行了快速的基准测试:

C#版本:

static void Main(string[] args)
{
    List<string> myList = new List<string>();

    for(int i = 0; i < 500000; i++)
    {
        myList.Add(i.ToString());
    }

    DateTime st = DateTime.Now;
    foreach(string s in myList)
    {
        Console.WriteLine(s);
    }
    DateTime et = DateTime.Now;

    Console.WriteLine(et - st);
    Console.ReadLine();
}

VB.NET版本:

Module Module1

    Sub Main()
        Dim myList As List(Of String) = New List(Of String)

        For i = 1 To 500000
            myList.Add(i)
        Next

        Dim st, et
        st = DateTime.Now
        For Each s As String In myList
            Console.WriteLine(s)
        Next
        et = DateTime.Now

        Console.WriteLine(et - st)
        Console.ReadLine()
    End Sub

End Module

在发布版本中(最重要的版本),执行500000次迭代,C# 代码稍微快一点,但只是一点点。

调试版本:

C#     - 1分40秒457毫秒
VB.NET - 1分42秒022毫秒

发布版本:

C#     - 0分56秒179毫秒
VB.NET - 0分56秒327毫秒

0

你应该进行一个实验。使用 (棒极了的) .NET Reflector,在每种语言中构建一个简单的测试案例,并查看生成的 MSIL 是否相同。


我不是C#程序员(我被安排参与一个C#项目,只是利用我的C++经验作为指导)。但如果我有时间,我会尝试一些实验。 - MrValdez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接