gpt4 book ai didi

c# - VB.NET 中的 foreach 是否比 c# 中的更快?

转载 作者:太空狗 更新时间:2023-10-29 23:55:29 25 4
gpt4 key购买 nike

我的同事说,在之前的采访中,他了解到 VB.Net 中的 foreach 比 c# 的 foreach 更快。他被告知这是因为两者具有不同的 CLR 实现。

从 C++ 的角度来看,我很好奇这是为什么,我被告知我需要先阅读 CLR。谷歌搜索 foreach 和 CLR 无法帮助我理解。

有没有人能很好地解释为什么 foreach 在 VB.Net 中比在 C# 中更快?还是我的同事误导了我?

最佳答案

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
}

关于c# - VB.NET 中的 foreach 是否比 c# 中的更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/637224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com