Shawn Weisfeld

I find when I talk to myself nobody listens. - Shawn Weisfeld
posts - 352, comments - 144, trackbacks - 34

My Links

News

The views expressed in this blog are mine and mine alone, not that of my employer, Microsoft, or anyone else’s. No warrantee is given for the quality of any material on this site.

Archives

Post Categories

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)

Print | posted on Wednesday, December 13, 2006 1:17 PM | Filed Under [ .NET C# VB.NET ]

Feedback

Gravatar

# 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.
12/18/2006 3:04 PM | Eamon
Gravatar

# 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 :-).
12/20/2006 1:02 PM | Eamon
Gravatar

# 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 eamon.nerbonne.org/.../...le-of-difficulty-of.html
12/21/2006 3:17 PM | sweisfeld
Gravatar

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

12/28/2006 10:43 PM | eamon.nerbonne.org
Gravatar

# Dependency ultram.

7/21/2008 4:58 PM | blogs.ign.com
Gravatar

# Vicodin.

7/30/2008 8:05 AM | blogs.ign.com
Gravatar

# Leistung von Visual Basic verbessern - EntwicklerGemein.de

9/27/2008 11:53 AM | www.entwicklergemein.de

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 2 and type the answer here:

Powered by: