开发者

Is C# faster than VB.NET? [closed]

开发者 https://www.devze.com 2023-01-18 13:00 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, r开发者_JAVA百科eferences,or expertise, but this question will likely so
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, r开发者_JAVA百科eferences, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 12 years ago.

You'd think both are the same.

But maybe it's the compiler that Microsoft has used, but I've noticed that when compiling two very small programs, identical logic. VB.NET uses more IL instructions.

Is it true than that c# must be faster, if only because its compiler is smarter.


This is really hard to respond to definitively with the limited amount of information available. It would help a lot if you provided the code from both samples and the compiler options used.

To answer the question though, no C# is not inherently faster. Both languages generate to IL and run on the CLR. For most features they even generate the same IL. There are differences for some similar features but they rarely add up to significant performance changes.

VB can appear slower if you run into some of the subtle differences in the languages and environment. A couple of common examples are ...

  • Many environments default to checked integer operations for VB.Net but not C#
  • Subtle coding issues can lead to late binding where it appears to be early binding
  • Believing switch and Select have the same semantics

Once these are removed the languages perform with very similar performance profiles.


The answer is yes and no. It really depends on what specific feature you are referring to. Likewise, there are areas where VB executes faster. I can give an example of each.

This code in VB...

For i As Integer = 0 To Convert.ToInt32(Math.Pow(10, 8))
Next

...is about 100x faster than this code in C#.

for (int i = 0; i <= Convert.ToInt32(Math.Pow(10, 8)); i++)
{
}

It is not that the VB compiler is better at generating code that executes for loops faster though. It is that VB computes the loop bound once while C# computes the loop condition on each iteration. It is just a fundamental difference in the way the languages were intended to be used.

This code is C#...

int value = 0;
for (int i = 0; i <= NUM_ITERATIONS; i++)
{
  value += 1;
}

...is slightly faster than the equivalent in VB.

Dim value As Integer = 0
For i As Integer = 0 To NUM_ITERATIONS
  value += 1
Next

The reason in this case is that the default behavior for VB is to perform overflow checking while C# does not.

I am sure there are other difference in the languages that demonstrate similiar performance biases. But, both languages are built on top of the CLR and both compile to the same IL. So making blanket statements like "Language X is faster than language Y" without adding the important qualifying "in situation Z" clause are simply incorrect.


C# match more close to IL than VB.NET

VB.NET sometimes do lot of things behind the scenes. Like On Error Resume Next, that write a try catch for each statement

But in general both have the same features and performance.

You can open your code in Reflector and see as C# code. Realize if C# code was what you expected


Make sure the programs really are identical. For example, depending on Options, these two lines are actually very different:

Dim x = "some string"

.

string x = "some string";

To match that C# code, the VB should look like this:

Dim x As String = "some string"


It sounds like the differences are purely in the compilers interpretation of the source code. A tech republic article comes to pretty much the same conclusion: https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-1027686.html


I haven't done any tests, but I think speed would be about the same. If anything select for coding style and syntax.

0

精彩评论

暂无评论...
验证码 换一张
取 消