Results 1 to 3 of 3

Thread: C# and VB.NET benchmark Comparison

  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

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3
    Thelonius
    Guest
    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.

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