Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
VB x C# with StringBuilder
Message
 
To
17/05/2002 07:50:26
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00658061
Message ID:
00658091
Views:
18
Hi Claudio,

When I ran your sample for the first time, I did get a shock. After careful examination, I found that you did not specify a DataType for the StringBuilder in the VB .NET code. Here is my VB .NET and C# test code and results. Notice that the results are the same in VB .NET and C#.

Test Results for VB .NET
Imports System.Text

Module Module1

    Public Sub Main()
        'Run the test 10 times
        Dim lnI As Integer
        For lnI = 1 To 10
            TestIt()
        Next

        'Add a break
        Console.ReadLine()
    End Sub

    Public Sub TestIt()
        Dim tStart As DateTime = DateTime.Now
        Dim SB As New StringBuilder()

        Dim i As Integer = 1
        While i <> 30000
            SB.Append("Hi" + i.ToString())
            SB.Append(" blah blahblah blahblah blahblah blahblah blahblah blah ")
            i = i + 1
        End While

        Console.WriteLine(DateTime.Now.Subtract(tStart))
    End Sub
    'Results:
    '00:00:00.3204608
    '00:00:00.2703888
    '00:00:00.2403456
    '00:00:00.2403456
    '00:00:00.2503600
    '00:00:00.2403456
    '00:00:00.2503600
    '00:00:00.2403456
    '00:00:00.2503600
    '00:00:00.2403456
End Module
Test results for C#
using System;
using System.Text;

namespace CSharpSpeedTest
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			for(int i = 0; i < 10; i++)
				PrintIt();

			//Add a break
			Console.ReadLine();
		}

		static void PrintIt()
		{
			DateTime tStart = DateTime.Now;
			StringBuilder SB = new StringBuilder();

			int i=1;

			while (i != 30000)
			{
				SB.Append("Hi"+i.ToString());
				SB.Append(" blah blahblah blahblah blahblah blahblah blahblah blah ");
				i++;
			}

			Console.WriteLine(DateTime.Now.Subtract(tStart));
		}

		//Results
		// 00:00:00.3304752
		// 00:00:00.2503600
		// 00:00:00.2503600
		// 00:00:00.2403456
		// 00:00:00.2603744
		// 00:00:00.2403456
		// 00:00:00.2503600
		// 00:00:00.2403456
		// 00:00:00.2503600
		// 00:00:00.2403456

	}
}
Kamal

>>Hi Claudio,
>
>Hi, Kamal.
>
>>This way the object "i" will not be boxed and unboxed everytime it is accessed. Try it now... the results should be the same.
>
>yup, the same result... have you tried both codes (C# and VB) in your box? I´m just impressed with the difference... here, C# is twenty times faster... that code took less than 1 second in C#, while in VB, it took 20 seconds....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform