VB.NET vs. C#: 效率直击

原创 2001年12月05日 16:38:00

 

从效率上看,是不是所有的.NET语言都拥有同样的执行效率呢?这里的答案可能使你惊讶。来看看Lamont Adams 从深层次代码分析得到的结果。

VB.NET vs. C#, round 2: Pounding on performance
Dec 3, 2001
Lamont Adams
Author's Bio | E-Mail | Archive

If there's one property that all new technologies share as they pass through their infancy, it's an excess of questions about them. Judging by the number of questions we received in response to our recent article "The three-million-programmer question: VB.NET or C#?," Microsoft's new .NET development framework is no different.

Hence, we decided to launch a new Developer Republic column, .NET Answers. Aside from using this space to challenge English punctuation rules on a regular basis, I hope to answer your questions about .NET development. So if you have a question about Microsoft's new development platform, send it my way. I'll take my best shot at it.

We received our inaugural question from member Vladimir Srecovic, from Yugoslavia, in response to the article referenced above. Vladimir has a question about performance.

C# performance benchmarking


Q: I've seen reports that C#-produced IL (Intermediate Language) code runs faster than VB.NET-produced code. Is this true?

Vladimir Srecovic


A: I was all set to respond to this question with a negative answer. I haven't seen anything that would point to C# having a speed advantage and therefore didn't think it likely that any significant performance difference would exist. After all, IL code is compiled to native code by the same Just In Time (JIT) compiler, regardless of which IL compiler generated it. So in theory at least, as long as your IL compiler was built to standard, equivalent VB.NET, C#, or even COBOL.NET code would compile into essentially the same IL code.

That's the conventional wisdom, anyway. Being the thorough type, though, I decided to perform a little experiment to see if this reasoning bore out. I took the VB.NET and C# versions of the TypeFinder sample application (look for it in /Program Files/Microsoft.Net/FrameworkSDK/Samples/applications/typefinder) and compiled them both. Next, I took the resulting executable files and ran them through the MSIL disassembler utility (/Program Files/Microsoft.Net/FrameworkSDK/Bin/ildasm.exe) to view the IL that both compilers produced. I then compared the two pieces of IL that were generated for the rather simple method IndentedWriter.WriteLine. You can see the VB.NET source for this method in Figure A. The C# source is shown in Figure B.

Figure A
r00820011207adm01_02.gif
The VB.NET version is slightly longer than the C# version.


Figure B
r00820011207adm01_01.gif
The C# version takes seven lines of code.



I was surprised by what I discovered when I compared the resulting IL code: The VB.NET version was nine lines (and 9 KB) longer than the C# version. You can see the IL that the VB.NET compiler generated in Listing A; the C# compiler's results appear in Listing B.

After comparing the code, I did a little quick and dirty benchmarking and found that over 12 trials, the C# version of FindType.exe, which uses reflection to list the methods belonging to a particular object, narrowly outperformed the VB.NET version. The latter's best time was slightly slower than the former's worst time.

What's going on here?


I'm no expert on IL, and it's currently poorly documented. But from looking at the IL code, it seems obvious that even though the two pieces of source are functionally identical and perform the same tasks in the same order, the resulting IL code is quite different:

  • Five of the extra opcodes generated by the VB.NET compiler are nop, which, according to Microsoft's current documentation, stands for "No Operation" or "pass."
  • The VB.NET IL declares an extra local variable of type int32 in the .locals section. This local variable is apparently used to cache a copy of the IndentedWriter.myIndent field for use in the for loop (line IL_002b). The C# IL, on the other hand, refers to the class field directly in line IL_0021).
  • The VB.NET IL uses six opcodes (IL_0000 to IL_000e) to create its StringBuilder object, while the C#-generated IL uses only five (IL_0000 to IL_000d). One of the VB.NET opcodes used here is a nop.
  • Both for loops are implemented in 13 opcodes (lines IL_001a through IL_002c for VB.NET and lines IL_0010 to IL_0026 for C#). Interestingly though, one of the opcodes in the VB.NET for loop is a nop.

Anyone care to explain this?
I would like to hear from any IL gurus that can more adequately explain what's going on here. Send me an e-mail with your explanation.


Something strange is afoot


Although there's no smoking gun here, it does appear that the VB.NET compiler generated slightly less efficient code than its C# counterpart. That would seem to support the conclusion that, at least in this example, C# does in fact outperform equivalent VB.NET code. All this could change since we are still dealing with a beta. So stay tuned. There may be another chapter to this story yet.

vb.net和c#的区别

用vb.net和c#都有一段时间了,虽然知道其中不仅仅只有语法的差异,但一直没时间一一研究到底有哪些不同,今天突然在网上看到一网友提供了此心得,遂转载。 //全局层 VB支持Module,是一种全...
  • cooska
  • cooska
  • 2013年01月10日 15:04
  • 4325

VB.NET and C# 的语法区别举例

VB.NET and C# Comparison This is a quick reference guide to highlight some key syntactical differen...
  • zhangyubishoulin
  • zhangyubishoulin
  • 2014年10月07日 13:25
  • 2342

vb.net与C#混用解决方案

所为混用不是为了能够在一个代码文件中同时书写C#和vb.net语句 而是可以在一个程序中发挥两种语言不同的优势 或者说 更容易发挥码农对不同语言的理解和长处 C/S 下的混合调用不会有任何阻碍 假如你...
  • feifei2666011
  • feifei2666011
  • 2013年09月04日 09:06
  • 2710

[C#.NET] 如何 使用 多執行緒 Thread / 跨執行緒 存取UI

http://www.dotblogs.com.tw/yc421206/archive/2009/02/13/7141.aspx System.Threading 命名空間提供了執行緒的建立...
  • rocky69
  • rocky69
  • 2013年02月06日 09:09
  • 9087

VB.NET动态加载C#或VB.NET生成的DLL文件调用动画教程[黑白智慧写](动态调用DLL).txt

 _________________下载原码:____________________________ -注意:全包括二部分,一定要下载完后,直接解压, 在解前不要修改压缩文件名,否不能正常...
  • action20
  • action20
  • 2014年01月25日 18:53
  • 1026

C#/VB.net 与本菜鸡一起摸索用图像引擎做游戏

随着硬件整体处理能力的提升,.net平台上的这些托管语言的运行效率问题逐渐变得几乎……额开玩笑的233,但是开发一个2D的一般规模的游戏还是很没问题的。JIT的优化做得还是不错的,在我看过CLR v...
  • u011190734
  • u011190734
  • 2016年08月04日 12:10
  • 582

Opencv实现击中击不中

《数字图像处理与机器视觉——Visual C++与Matlab实现》P289公式8-7明显错误,但是后面的图是正确的。 详细知识可以看击中击不中,里面的图示跟书本中介绍的差不多。 另外书本中介绍:背景...
  • u012033076
  • u012033076
  • 2014年03月27日 09:25
  • 1600

击中击不中变换的作用理解

学习形态学变换的时候,看到击中击不中变换一直百思不得其解,虽然他的定义写的很清楚明白,但是不知道具体是干嘛用的,按照自己的理解写了一个matlab小程序来试试。 1、击中击不中变换是形态学形状检测的...
  • horseinch
  • horseinch
  • 2015年12月01日 13:17
  • 6427

Opencv图像识别从零到精通(18)-------击中击不中

在我们学习了膨胀腐蚀和基于膨胀腐蚀的变化之后,我比较喜欢的一个是击中击不中,因为喜欢所以就要单独列出来,心里总是觉得他可以有很多的用处,以后模版匹配,特征检测都会用,更深入的是,他会加深对膨胀腐蚀的理...
  • qq_20823641
  • qq_20823641
  • 2016年07月30日 13:59
  • 2926

图像形态学处理(4. 击中击不中变换)

连通子图的匹配定位,像素模式的匹配定位
  • stscsdn
  • stscsdn
  • 2016年09月11日 21:29
  • 1742
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:VB.NET vs. C#: 效率直击
举报原因:
原因补充:

(最多只允许输入30个字)