Results 1 to 3 of 3

Thread: C# and VB.NET benchmark Comparison

Threaded View

  1. #1

    Thread Starter
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256

    C# and VB.NET benchmark Comparison

    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:
    1. Option Explicit On
    2. Imports System
    3.  
    4. Public Class Form1
    5.     Inherits System.Windows.Forms.Form
    6.  
    7.     Dim num As Long = 1000000000
    8.     Dim dt As DateTime
    9.     Dim ts As TimeSpan
    10.     Dim timeNow As String = ""
    11.     Dim i As Long
    12.  
    13. #Region " Windows Form Designer generated code "
    14.  
    15.     Public Sub New()
    16.         MyBase.New()
    17.  
    18.         'This call is required by the Windows Form Designer.
    19.         InitializeComponent()
    20.  
    21.         'Add any initialization after the InitializeComponent() call
    22.  
    23.     End Sub
    24.  
    25.     'Form overrides dispose to clean up the component list.
    26.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    27.         If disposing Then
    28.             If Not (components Is Nothing) Then
    29.                 components.Dispose()
    30.             End If
    31.         End If
    32.         MyBase.Dispose(disposing)
    33.     End Sub
    34.  
    35.     'Required by the Windows Form Designer
    36.     Private components As System.ComponentModel.IContainer
    37.  
    38.     'NOTE: The following procedure is required by the Windows Form Designer
    39.     'It can be modified using the Windows Form Designer.  
    40.     'Do not modify it using the code editor.
    41.     Friend WithEvents Button1 As System.Windows.Forms.Button
    42.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    43.         Me.Button1 = New System.Windows.Forms.Button()
    44.         Me.SuspendLayout()
    45.         '
    46.         'Button1
    47.         '
    48.         Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System
    49.         Me.Button1.Location = New System.Drawing.Point(80, 40)
    50.         Me.Button1.Name = "Button1"
    51.         Me.Button1.TabIndex = 0
    52.         Me.Button1.Text = "Go"
    53.         '
    54.         'Form1
    55.         '
    56.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    57.         Me.ClientSize = New System.Drawing.Size(240, 84)
    58.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})
    59.         Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
    60.         Me.MaximizeBox = False
    61.         Me.Name = "Form1"
    62.         Me.Text = "VB.NET Version"
    63.         Me.ResumeLayout(False)
    64.  
    65.     End Sub
    66.  
    67. #End Region
    68.  
    69.  
    70.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    71.  
    72.         Form1.ActiveForm.Cursor = Cursors.WaitCursor
    73.         Button1.Enabled = False
    74.         dt = DateTime.Now
    75.  
    76.         For i = 1 To num
    77.         Next i
    78.  
    79.         ts = DateTime.Now.Subtract(dt)
    80.         Form1.ActiveForm.Cursor = Cursors.Default
    81.         Button1.Enabled = True
    82.         timeNow = ts.Minutes & " Min " & ts.Seconds & " Sec(s) " & ts.Milliseconds & " Milliseconds"
    83.         MessageBox.Show(timeNow, "Loop Completed")
    84.     End Sub
    85. End Class

    C# Version
    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");		
    		}		
    	}
    }
    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.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width