tags:

views:

328

answers:

6

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.

+3  A: 

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

Fujiy
C# also does a lot behind the scenes, for example, `using`, `lock`, `foreach`. The compiler can also dramatically alter your code to improve performance.
ChaosPandion
Yes, but in general is more close than VB.NET
Fujiy
-1. Are closures and `yield return` close to IL?? No, the compiler creates [loads of IL](http://blogs.msdn.com/b/oldnewthing/archive/2006/08/02/686456.aspx), sometimes including entire new classes, to support [one or two C# statements](http://startbigthinksmall.wordpress.com/2008/06/09/behind-the-scenes-of-the-c-yield-keyword/). C# is way different from IL, and a good thing too! People used to say **C** was close to **assembly language**, is that what you are thinking of?
MarkJ
I just say that is more close than VB.NET. Not that is equal IL....
Fujiy
@Fujiy well I don't think that's correct either, and anyway it's irrelevant to whether C# code is faster/better.
MarkJ
+2  A: 

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"
Joel Coehoorn
With Option Infer On in VB, aren't those too lines identical?
Meta-Knight
@Meta Yes, hence the "depending on options" phrase. I'll make two edits to clarify it, though: 1) uppercase Options 2) var -> string.
Joel Coehoorn
@Meta-Night yes they are identical with Option Infer On. But that is only the default for new projects starting in 2008 and above. Old projects still upgrade with Option Infer Off to prevent conversion issues with late binding.
JaredPar
A: 

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: http://articles.techrepublic.com.com/5100-10878_11-1027686.html

Kyle
A: 

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

Anish
+11  A: 

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.

JaredPar
+2  A: 

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.

Brian Gideon