PDA

Click to See Complete Forum and Search --> : C# and VB.NET benchmark Comparison


DevGrp
May 12th, 2002, 01:56 PM
Hey guys, just thougt I would share this.

I did a little benchmarking with vb.net and C#. Its nothing big, just a simple for loop iterating 1 billion times. The code is below.

I noticed something interesting. If I put the variable declarations inside the button_click method, it adds at least 2 seconds to the time it takes the loop to complete, but if they are declared above the contructor, those 2 seconds are shaved off.

VB.NET Version

Option Explicit On
Imports System

Public Class Form1
Inherits System.Windows.Forms.Form

Dim num As Long = 1000000000
Dim dt As DateTime
Dim ts As TimeSpan
Dim timeNow As String = ""
Dim i As Long

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Button1.Location = New System.Drawing.Point(80, 40)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Go"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(240, 84)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.Name = "Form1"
Me.Text = "VB.NET Version"
Me.ResumeLayout(False)

End Sub

#End Region


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Form1.ActiveForm.Cursor = Cursors.WaitCursor
Button1.Enabled = False
dt = DateTime.Now

For i = 1 To num
Next i

ts = DateTime.Now.Subtract(dt)
Form1.ActiveForm.Cursor = Cursors.Default
Button1.Enabled = True
timeNow = ts.Minutes & " Min " & ts.Seconds & " Sec(s) " & ts.Milliseconds & " Milliseconds"
MessageBox.Show(timeNow, "Loop Completed")
End Sub
End Class


C# Version

using System;
using System.Windows.Forms;
namespace TestCSharp
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;

private DateTime dt;
private TimeSpan ts;
private long num = 1000000000;
private string timeNow = "";

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 48);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Go";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(240, 84);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "C# Version";
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
Form1.ActiveForm.Cursor = Cursors.WaitCursor;
dt = DateTime.Now;
button1.Enabled = false;

for(long i = 1; i <= num; i++);

ts = DateTime.Now.Subtract(dt);
Form1.ActiveForm.Cursor = Cursors.Default;
button1.Enabled = true;

timeNow = ts.Minutes + " Min " + ts.Seconds + " Sec(s) " + ts.Milliseconds + " Milliseconds";

MessageBox.Show(timeNow, "Loop Completed");
}
}
}


These are the times I get on my system:
P4 1.7 Ghz
512 MB Ram
WindowsXP

C# - 5 Seconds, 437 Milliseconds
VB.NET - 7 Seconds, 46 Milliseconds

Here are files to do the test. Both versions are included.

MrPolite
May 12th, 2002, 05:24 PM
I didnt look at the code or anything. But I was just wondering if you tried the test several times or not, because it might give you different results each time.
Any ways, C# is SLIGHLY faster than VB.NET:(
this might be intersting for some of you guys:
C# code might run slightly faster in a few circumstances; for example, the C# compilter generates code
that reclaims the memory used by objects more aggressively than under Visual Basic. However, most of the
time the difference in the performance won't be greater than 5 percent, so it's hardly an argument for
selecting one language over the other.(from Programming Microsoft Visual Basic.Net)

maybe it has to do something with memory stuff that he says:rolleyes:

Thelonius
May 12th, 2002, 10:20 PM
I wonder if the backward compatibility of VB code that VBdN can use factors into it's slightly slower performance; even if a VB6 style syntax is not used. Even if C# is less than 5% faster on a benchmark test, it's still pretty impressive for VB, considering the total make over it got. C# is a great language, but I have yet to see a significant number of companies looking to totally rewrite a VB app just to use C# (with the exception of ASP to ASP.NET in which you have to re-write regardless). The company I work for has a VB app with roughly 3 Million lines of code that has been written over a course of 7 years in the various releases of VB. A rewrite of our main revenue producing app in C# would probably put us out of business. That 5% decrease is, IMO, pretty easy to live with considering 99% of our processes requiring optimum speed performance are going to be on a server.