C#和VB.NET效率的测试,请高手给解释一下这个测试结果
我们知道在.NET中,不管用VB.NET,C++,还是C#写的程序,都会被编译成CLR可以执行的中间代码,那么如果不同的语言中做同样的事情,编译出
来的IL(中间代码)是否相同的?
我做了如下的测试,发现了一个问题。
C#写的语句编译出来的IL看上去一切正常,但VB.NET就不对了,在实际代码前后,插入了3个不应该出现的nop语句,这不是降低了VB.NET的效
率吗?
按理说,如果你写的程序在CLR上跑的话,由于中间代码的存在,VB.NET C#的效率应该是一样的(至少对于本例来说是这样的),但现在不知
为什么VB生成的中间代码会有一些不应该有的语句?
那位高手知道原因的,烦请给在下解释一下。
在下认为,CLR运行IL的时候,应该会忽略nop语句,但我不敢确定。
程序编译环境
Visual Studio .NET Beta 2,中文版
CLR version is v1.0.2914
如果那位有RC3/4的话,麻烦给测试一下,看看IL输出的语句时候还是这样
(IL语句可以IL DASM <IL Disassembler> 得到)
/////////////////////This is the Vb.NET code/////////////////////
Module Module1
Sub Main()
System.Console.WriteLine("This is VB.NET")
End Sub
End Module
/////////////////////This is the IL code of the above code/////////////////////
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 14 (0xe)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "This is VB.NET"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: nop
IL_000d: ret
} // end of method Module1::Main
/////////////////////C# code which do the same thing like the above VB.NET code/////////////////////
namespace Test
{
class Test
{
static void Main(string[] args)
{
System.Console.WriteLine("This is C#");
}
}
}
/////////////////////The following is the IL code of C# code/////////////////////
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 11 (0xb)
.maxstack 8
IL_0000: ldstr "This is C#"
IL_0005: call void [mscorlib]System.Console::WriteLine(string)
IL_000a: ret
} // end of method Test::Main