VB.NET VS. C #: Efficiency Direct

zhaozj2021-02-08  258

From the efficiency, is it all .NET language has the same efficiency? The answers here may surprise you. Take a look at the results obtained from Deep Code Analysis from Deep Code.

VB.NET VS. C #, ROUND 2: Pounding On Performance Dec 3, 2001lamont 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 Reference Above. VLADIMIR HAS A Question About Performance.c # Performance Benchmarking

Q: I'VE Seen Reports That C # -ProducesD IL (Intermediate Language) Code Runs Faster Than Vb.Net-Product Code. Is this True?

VLADIMIR SRECOVIC

A: I was all set to respond to this question with a negative answer I have not seen anything that would point to C # having a speed advantage and therefore did not 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 / FRAM Eworksdk / bin / ildasm.exe) to view the il That Both Compilers Product, Il That Wee 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 athe vb.net version is slightly longer than the c # version.

Figure Bthe C # Version Takes Seven Lines of Code.

I Was surprised by What i discovered by: the vb.net version: The vb.net version WAS NINE LINES (AND 9 KB) LONGER THAN THEN THEN THATED. 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 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 what 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.

转载请注明原文地址:https://www.9cbs.com/read-952.html

New Post(0)