C#和VB.NET效率的测试,请高手给解释一下这个测试结果

yongyu2000 2002-01-12 07:56:07
我们知道在.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
...全文
147 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqchen79 2002-01-14
  • 打赏
  • 举报
回复
这主要是VB.NET编译器的问题,在cmd下打命令vbc可以察看其版本。
yongyu2000 2002-01-13
  • 打赏
  • 举报
回复
这是我今天在 RC4 版本上到的IL输出,
.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"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: nop
IL_000d: ret
} // end of method Module1::Main

nop依然存在。
VS.NET 版本是7.0.9447
Framework的版本是1.0.3617
怎么获得CLR,和Compiler的版本?
Soft21 2002-01-13
  • 打赏
  • 举报
回复
大家好啊,我嘛,这里还是有高手的嘛
qqchen79 2002-01-13
  • 打赏
  • 举报
回复
重要的是VBC的版本啦:
Microsoft (R) Visual Basic.NET Compiler version 7.00.9254
for Microsoft (R) .NET CLR version 1.00.2914.16
qqchen79 2002-01-13
  • 打赏
  • 举报
回复
我的Beta2 Framework 版本是v1.0.2914,VB.NET的IL里已经没有nop指令了。
vbc test.vbs
vbc /optimize+ test.vbs
结果相同
我相信这只是Beta阶段的一点困难而已,M$最终能够从VB.NET中去掉的。

至于VB.NET和C#性能的比较,参见:
http://www.csdn.net/develop/article%5C11%5C11483.shtm
和下面我的评论。:)

Ninputer 2002-01-13
  • 打赏
  • 举报
回复
最新结果(RC5):大家的担心都是多余的,请看
Visual Basic

.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 11 (0xb)
.maxstack 8
IL_0000: ldstr "This is VB.net"
IL_0005: call void [mscorlib]System.Console::WriteLine(string)
IL_000a: ret
} // end of method CMain::Main

Visual C#

.method public hidebysig static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 11 (0xb)
.maxstack 1
IL_0000: ldstr "This is C#"
IL_0005: call void [mscorlib]System.Console::WriteLine(string)
IL_000a: ret
} // end of method Class1::Main

完全一样!!另外一个值得注意的问题是VB会自动添加STAThread属性优化,C#却不会。这是源代码
Public Class CMain
Public Shared Sub Main()
'Output something
Console.WriteLine("This is VB.net")
End Sub
End Class

class Class1
{
[STAThread]
public static void Main()
{
Console.WriteLine("This is C#");
}
}
Ninputer 2002-01-13
  • 打赏
  • 举报
回复
这么简单的语法,多一个nop无关紧要的,这并不是执行时的本机代码。
经过我的测试,If语句,While语句VB实现得比C#好,Try语句,Select语句C#实现得比VB好。总的来说无所谓啦!

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧