从效率上看,是不是所有的.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
Figure A |
The VB.NET version is slightly longer than the C# version. |
Figure B |
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:
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.
本文地址:http://com.8s8s.com/it/it46220.htm