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) |