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;
}
}