VB.NET vs. C#.NET

Ok so I know that this is a heated subject but I thought I would weigh in with my opinion and hopefully add some to the conversation. The focus of my discussion is not the “syntactical sugar” as one of my colleagues calls it but actual performance. To keep things the same I wrote two chunks of code one in VB the other in C# compiled them using VS.NET in release mode and then used Reflector (http://www.aisto.com/roeder/dotnet/) to decompile the assemblies. As you can see both versions produced the same disassembly but do they produce the same performance. Conventional wisdom would say yes, but my crude benchmarking shows otherwise. This would not bother me so much if I could explain it, but I cannot. What do you think, post your comments below. . .

VB Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    Sub Main()
Dim num1 As Long = 0
Do While (num1 < 10)
Dim time2 As DateTime = DateTime.Now
Dim num2 As Long = 0
Dim num3 As Long = 0
Dim num4 As Long = 0
Dim num5 As Long = 0
Dim num6 As Long = 0
Dim num7 As Long = 0
Do While (num7 < 100000000)
num5 = ((num2 + (num3 * num4)) + num5)
num6 = (((((num4 * num3) * 12) + num2) + (num2 + (num3 * num4))) + num6)
num7 += 1
Loop
Dim span1 As TimeSpan = DirectCast((DateTime.Now - time2), TimeSpan)
Console.WriteLine("VB {1} {0:0.000} {2} {3}", New Object() {span1.TotalMilliseconds, num1, num5, num6})
num1 += 1
Loop
Console.ReadKey()
End Sub

C# Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
        static void Main(string[] args)
{
for (long num1 = 0; num1 < 10; num1++)
{
DateTime time2 = DateTime.Now;
long num2 = 0;
long num3 = 0;
long num4 = 0;
long num5 = 0;
long num6 = 0;
for (long num7 = 0; num7 < 0x5f5e100; num7++)
{
num5 = (num2 + (num3 * num4)) + num5;
num6 = ((((num4 * num3) * 12) + num2) + (num2 + (num3 * num4))) + num6;
}
TimeSpan span1 = (TimeSpan)(DateTime.Now - time2);
Console.WriteLine("C# {1} {0:0.000} {2} {3}", new object[] { span1.TotalMilliseconds, num1, num5, num6 });
}
Console.ReadKey();
}

VB Code Disassembled (into C#)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static void Main()
{
for (long num1 = 0; num1 < 10; num1++)
{
DateTime time1 = DateTime.Now;
long num2 = 0;
long num3 = 0;
long num4 = 0;
long num5 = 0;
long num6 = 0;
for (long num7 = 0; num7 < 0x5f5e100; num7++)
{
num5 = (num2 + (num3 * num4)) + num5;
num6 = ((((num4 * num3) * 12) + num2) + (num2 + (num3 * num4))) + num6;
}
TimeSpan span1 = (TimeSpan) (DateTime.Now - time1);
Console.WriteLine("VB {1} {0:0.000} {2} {3}", new object[] { span1.TotalMilliseconds, num1, num5, num6 });
}
Console.ReadKey();
}

C# Code Disassembled (into C#)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
private static void Main(string[] args)
{
for (long num1 = 0; num1 < 10; num1++)
{
DateTime time1 = DateTime.Now;
long num2 = 0;
long num3 = 0;
long num4 = 0;
long num5 = 0;
long num6 = 0;
for (long num7 = 0; num7 < 0x5f5e100; num7++)
{
num5 = (num2 + (num3 * num4)) + num5;
num6 = ((((num4 * num3) * 12) + num2) + (num2 + (num3 * num4))) + num6;
}
TimeSpan span1 = (TimeSpan) (DateTime.Now - time1);
Console.WriteLine("C# {1} {0:0.000} {2} {3}", new object[] { span1.TotalMilliseconds, num1, num5, num6 });
}
Console.ReadKey();
}

Performance results

Language Try Time Language Try Time
VB 0  6,369.35 C# 0   2,718.63
VB 1  6,353.82 C# 1   2,687.56
VB 2  6,369.35 C# 2   2,687.56
VB 3  6,369.35 C# 3   2,703.09
VB 4  6,353.82 C# 4   2,687.56
VB 5  6,369.35 C# 5   2,687.56
VB 6  6,353.82 C# 6   2,703.09
VB 7  6,369.35 C# 7   2,689.58
VB 8  6,369.35 C# 8   2,698.18
VB 9  6,353.82 C# 9   2,698.18
AVERAGE  6,363.14 AVERAGE   2,696.10
(times in milliseconds)

Published Wednesday, December 13, 2006 8:17 AM by sweisfeld
Filed under: , ,

Comments

# re: VB.NET vs. C#.NET

It's certainly odd that performance differs so greatly.

Maybe this is caused (at least partially) by the weird code you're writing, since it doesn't actually do _anything_ right?  This might just be some small compiler corner case.

Furthermore; you're using long's and that too seems unusual.

Monday, December 18, 2006 9:04 AM by Eamon

# re: VB.NET vs. C#.NET

I played around with your benchmark and discovered that integer overflow checking is at fault - with that off, VB is just as fast.  I wrote a post about it too :-).

Wednesday, December 20, 2006 7:02 AM by Eamon

# re: VB.NET vs. C#.NET

Eamon I think you hit the nail on the head. Checking the 'Remove Integer Overflow Checks' options under Properties | Compile | Advanced Compile Options allowed my VB Code to consistently perform the test in under 3 seconds. Good Catch.

You can read Eamon’s full post at the following URL http://eamon.nerbonne.org/2006/12/vbnet-vs-c-example-of-difficulty-of.html

Thursday, December 21, 2006 9:17 AM by sweisfeld

# http://eamon.nerbonne.org/2006/12/vbnet-vs-c-example-of-difficulty-of.html

Thursday, December 28, 2006 4:43 PM by TrackBack

# Dependency ultram.

Ultram. Order ultram no prescription. Tramadol ultram and acute pain.

Monday, July 21, 2008 11:58 AM by Ultram.

# Vicodin.

Vicodin. Vicodin withdrawal.

Wednesday, July 30, 2008 3:05 AM by No prescription vicodin.

# Leistung von Visual Basic verbessern - EntwicklerGemein.de

Pingback from  Leistung von Visual Basic verbessern - EntwicklerGemein.de

Saturday, September 27, 2008 6:53 AM by Leistung von Visual Basic verbessern - EntwicklerGemein.de