General discussion

Locked

VB Net is Faster Than C#?

By guoc ·
Our software has several modules with heavy calculation written by VB6. Before migrate to dot net, we would like to find out which is the better language to adapt to. Since most developers prefer C#, the test itself is symbolic. To avoid any side-effect, the test includes pure calculations only. There are no arrays, collections, or strings. The test is conducted using 3 languages, VB6, C# & VBNet. On the main forms of each one, a text box keeps the times of iterations (100000000), other textbox to show the elapsed time, and a command button to execute. I did not use machine frequency or ticker to count time. Here is the VB code written in the click event of the command button.

Dim i As Long
Dim iDiff As Integer iter = CLng(txtIterations.Text)

dA = 10.12
dB = 3.5678
dC = 12.845
DTStart = Now

For i = 1 To iter
dR = dA + dB - dC
dR = dA * dB / dC
dR = dA - dB + dC
dR = dA / dB * dCNext

DTEnd = Now
iDiff = DateDiff("s", DTStart, DTEnd)

txtTimeUsed = iDiff

It is quite easy to duplicate the codes in C# & VBNet where dR, dA, dB, & dC are double with module scope. The variables are declared under samescope in each language.

The results are surprising for this specific test.
C# and VB6 Native Code spent same time to finish the iteration, while VBNet is 2 times faster (spent mush less time) than C# & VB6 both.
Is any one there would like to tell me why? It will be highly appreciated.

CG

This conversation is currently closed to new comments.

8 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

dubious

by ge.goodman In reply to VB Net is Faster Than C#?

VB6 did it at the same speed as C# ? This I find hard to believe. VB6's compiler as nowhere near as refined as C#/VB.NET's. I can only assume the overhead of JIT producing the native code EXE got in the way of your timings.

In theory there should be no performance differences between C# and VB.NET, as they both compile down to MSIL before they are turned into native code. Assuming you know C# as well as you know VB & VB.NET, roughly the same MSIL should be generated by the respective compilers for the "same" code.

I personally prefer C# to VB.NET, as it's a terse language and I like terse languages; also it holds your hand less than VB.NET, which I prefer; VB.NET seems a bit of a rush job to me (pretty printing ?? DirectCast ? CType ? CStr ? CInt ? urgh).

Most of the .NET framework classes were written in C# - what does that tell you about Microsoft's view of the two languages ?

But it all comes down to preference IMO - you can do the same things in either language, and you're programming against the same framework regardless.
Any differences are likely to be in the code itself. *Post the actual code you tested*

Also note that yours is hardly the most scientific method of testing performance differences between any language.

Collapse -

re dubius

by xcgeek In reply to dubious

"Also note that yours is hardly the most scientific method of testing performance differences between any language."

Scientific or not, it's a real test and it shows real results. I found the same thing between ADO and DAO (DAO being way faster) I am glad someone actually did a real life test just to see what would happen. I am interested to find out if anyone knows why.

Collapse -

Not the result I get

by whoiskevin In reply to VB Net is Faster Than C#?

I put together the VB6 application and an equivalent C# application. I get 14 seconds on VB6 and 12 seconds on C#. I didn't make the VB.net application after I got those results because I believe something in your original test is flawed.
Drop me a note (kbrown domain: whoiskevin.com) if you want to get more details/code.

Collapse -

C# twice as Fast as VB.Net in these versions...

by mtnmann In reply to Not the result I get

I attached the code I wrote (windows Forms) to simulate the code in the original post. I ran the VB.Net app at 100,000,000 iterations between 16 and 19 Seconds. The C# app ran between 6 and 8 seconds with the same number of iterations. I am at a loss for the difference when the byte code should be very similar... I tried using all doubles in the VB and it just seemed to run longer. Any ideas?

=============
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Long
Dim iDiff As Integer
Dim iter As Long
iter = CLng(txtIterations.Text)

Dim dA As Double = 10.12
Dim dB As Double = 3.5678
Dim dC As Double = 12.845
Dim DTStart As Date = Now
Dim dR As Double


For i = 1 To iter
dR = dA + dB - dC
dR = dA * dB / dC
dR = dA - dB + dC
dR = dA / dB * i

Next i

Dim DTEnd As Date = Now
iDiff = DateDiff(DateInterval.Second, DTStart, DTEnd)

Label1.Text = CStr(DTStart) & ", " & CStr(DTEnd) & " -- " & CStr(iDiff) & " Seconds"


End Sub

========== c# =================

private void button1_Click(object sender, System.EventArgs e)
{
double i;
double iter = Convert.ToDouble(txtIterations.Text);
double dA = 10.12;
double dB = 3.5678;
double dC = 12.845;
System.DateTime moment = System.DateTime.Now;

// Minute gets 57.
int min1 = moment.Minute;

// Second gets 32.
int sec1 = moment.Second;

// Millisecond gets 11.
int ml01 = moment.Millisecond;

double dR = 0;

for(i=1;i<=iter;i++)
{
dR = dA + dB - dC;
dR = dA * dB / dC;
dR = dA - dB + dC;
dR = dA / dB * i;
}

label1.Text = Convert.ToString(dR);
label2.Text = min1 + ":" + sec1 + ":" + ml01;
moment = System.DateTime.Now;
int min3 = moment.Minute;
int sec3 = moment.Second;
int ml03 = moment.Millisecond;

label3.Text = min3 + ":" + sec3 + ":" + ml03;
}

Collapse -

Console Versions of the Above C#/VB.NET code

by mtnmann In reply to C# twice as Fast as VB.Ne ...

Usage moduleName <# of iterations to perform>
=========================VB.NET===================
Imports System


Public Class Timing


Public Shared Sub Main(ByVal CmdArgs() As String)

System.Console.Out.WriteLine(CmdArgs(0))


If (True) Then
Dim i As Double
Dim iter As Double = cDbl(CmdArgs(0))
Dim dA As Double = 10.12
Dim dB As Double = 3.5678
Dim dC As Double = 12.845

Dim moment As System.DateTime = System.DateTime.Now

Dim min1 As Integer = moment.Minute
Dim sec1 As Integer = moment.Second
Dim ml01 As Integer = moment.Millisecond

Dim dR As Double = 0

For i = 1 To iter
dR = dA + dB - dC
dR = dA * dB / dC
dR = dA - dB + dC
dR = dA / dB * i
Next i

System.Console.Out.WriteLine(cStr(dR))
System.Console.Out.WriteLine(cStr(min1) & ":" & cStr(sec1) & ":" & cStr(ml01))
moment = System.DateTime.Now
Dim min3 As Integer = moment.Minute
Dim sec3 As Integer = moment.Second
Dim ml03 As Integer = moment.Millisecond

System.Console.Out.WriteLine(cStr(min3) + ":" + cStr(sec3) + ":" + cStr(ml03))
End If

End Sub 'Main
End Class 'Timing

==================== C# =======================
// Timing.cs
//

using System;
public class Timing {

public static void Main(System.String[] args) {
System.Console.Out.WriteLine(args[0]);
Timing a = new Timing();
a.RunTiming(args[0]);
}
public void RunTiming(String s)

{
double i;
double iter = Convert.ToDouble(s);
double dA = 10.12;
double dB = 3.5678;
double dC = 12.845;

System.DateTime moment = System.DateTime.Now;

int min1 = moment.Minute;
int sec1 = moment.Second;
int ml01 = moment.Millisecond;

double dR = 0;

for(i=1;i<=iter;i++)
{
dR = dA + dB - dC;
dR = dA * dB / dC;
dR = dA - dB + dC;
dR = dA / dB * i;
}

System.Console.Out.WriteLine(Convert.ToString(dR));
System.Console.Out.WriteLine(min1 + ":" + sec1 + ":" + ml01);
moment = System.DateTime.Now;
int min3 = moment.Minute;
int sec3 = moment.Second;
int ml03 = moment.Millisecond;

System.Console.Out.WriteLine( min3 + ":" + sec3 + ":" + ml03);
}
}

Collapse -

Look at the Byte code!

by dmcgary In reply to VB Net is Faster Than C#?

All of the .net compiles to MSIL (Byte code). Use a diff tool on the output. I think it is the data types were not the same. Is it VB6 or .net code you posted?

Collapse -

Speed Demon

by pelikan In reply to Look at the Byte code!

All of this is academic (in a perjorative sense). If you need raw speed (and this is becoming unnecessary as hardware doubles in efficiency every 5 years or so) you can sacrifice flexibility and compile C# or VBNET to native machine code - the efficiency of the code will rival if not equal optimized C++ (for either).

Using legacy technology is probably the slowest and most expensive proposition.

Collapse -

Reverse Engineering

by JohanGroenewald777 In reply to Look at the Byte code!

Best way to explain this is to Reverse Engineer the compiled code.

Original code:
------------------------------------

VB:
------------------------------------
Dim i As Integer
Dim iDiff As Integer
Dim iter As Integer = 10000000
Dim dA As Decimal
Dim dB As Decimal
Dim dC As Decimal
Dim dR As Decimal
Dim dCNext As Decimal = 2

dA = 10.12
dB = 3.5678
dC = 12.845

For i = 1 To iter
dR = dA + dB - dC
dR = dA * dB / dC
dR = dA - dB + dC
dR = dA / dB * dCNext
Next


C#:
------------------------------------
Int32 i;
Int32 iter = 10000000;
decimal dA;
decimal dB;
decimal dC;
decimal dR;
decimal dCNext = 2;

dA = 10.12M;
dB = 3.5678M;
dC = 12.845M;

for (i=1; i<= iter; i++)
{
dR = dA + dB - dC;
dR = dA * dB / dC;
dR = dA - dB + dC;
dR = dA / dB * dCNext;
}

Results after test:
------------------------------------
VB.NET: 8.7186942
C#: 9.2655657

Reverse Engineer the Code:
-----------------------------------------------

VB.NET:
------------------------------------

int num8 = 10000000;
decimal num4 = new decimal(2);
decimal num1 = new decimal(0x3f4, 0, 0, false, 2);
decimal num2 = new decimal(0x8b5e, 0, 0, false, 4);
decimal num3 = new decimal(0x322d, 0, 0, false, 3);
int num9 = num8;
for (int num6 = 1; num6 <= num9; num6++)
{
decimal num5 = decimal.Subtract(decimal.Add(num1, num2), num3);
num5 = decimal.Divide(decimal.Multiply(num1, num2), num3);
num5 = decimal.Add(decimal.Subtract(num1, num2), num3);
num5 = decimal.Multiply(decimal.Divide(num1, num2), num4);
}

C#:
------------------------------------

int num2 = 10000000;
decimal num7 = new decimal(2);
decimal num3 = new decimal(0x3f4, 0, 0, false, 2);
decimal num4 = new decimal(0x8b5e, 0, 0, false, 4);
decimal num5 = new decimal(0x322d, 0, 0, false, 3);
for (int num1 = 1; num1 <= num2; num1++)
{
decimal num6 = (num3 + num4) - num5;
num6 = (num3 * num4) / num5;
num6 = (num3 - num4) + num5;
num6 = (num3 / num4) * num7;
}

Results after using the reverse engineered code in C#:
------------------------------------
8.7343191 (very close to VB.NET)

Conclusion:
------------------------------------

1. It seems like compiled C# code use operators (+, -, /, *) (similar to the actual code that was written by the programmer. VB.NET converts the operators into syntax without any operators.
2. Methods are quicker than operators. (Only in this test. Prove me wrong with some code.)

Please remember:
These are only my findings and I tested with only 1 example (as above). I?m sure if we use different scenarios we might find other results.

Back to Windows Forum
8 total posts (Page 1 of 1)  

Related Discussions

Related Forums