Navigation

Search

Categories

On this page

Speed Testing Pattern

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 120
This Year: 1
This Month: 0
This Week: 0
Comments: 40

Sign In
Pick a theme:

# Wednesday, April 18, 2007
Wednesday, April 18, 2007 8:36:40 AM (Mountain Daylight Time, UTC-06:00) ( )

I do not remember where I found this. I like it though. Props to whoever did this.

using System;
using System.Diagnostics;

public class TestHarness
{
private static Stopwatch _watch;
public static int algo = 0;
public TestHarness()
{
_watch = new Stopwatch();
}
public static long RunTest(int test)
{
if (_watch == null)
_watch = new Stopwatch();
string str = string.Empty;
switch (test)
{
case 1:
_watch.Start();
str = String.Empty;
for (int i = 0; i < 10000; i++)
{
str = str + "blue_toothbrush";
}
_watch.Stop();
break;
case 2:
_watch.Start();
System.Text.StringBuilder SB =
new System.Text.StringBuilder(String.Empty);
for (int i = 0; i < 10000; i++)
{
str = str + "blue_toothbrush";
}
_watch.Stop();
break;
default:
break;
}

long elapsed = _watch.ElapsedMilliseconds;
_watch.Reset();
return elapsed;
}
}


class Program {
public static int Main(string[] args)
{
long elapsed = 0;
for (TestHarness.algo = 1; TestHarness.algo <= 2; TestHarness.algo++)
{
Console.WriteLine("Commencing test of algo {0}", TestHarness.algo);
elapsed = TestHarness.RunTest(TestHarness.algo);
Console.WriteLine("Test of algo {0} took {1:#.00} seconds", TestHarness.algo, (double)elapsed/1000);
}
return 0;
}
}

Comments [0] | | #