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
VB Code:
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
These are the times I get on my system:Code: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"); } } }
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.




Reply With Quote