Just sharing some of my inconsequential lunch conversations with you... RSS  

Wednesday, January 16, 2008

Benchmarking helper

As lazy as a programmer should always be, I've created a ConsoleStopWatch class for my previous article:

using (ConsoleStopwatch consoleStopwatch = new ConsoleStopwatch("sumOf += myHugeArray[i, j, k, l]"))
{
for (int i = 0; i < DIM0; i++)
{
for (int j = 0; j < DIM1; j++)
{
for (int k = 0; k < DIM2; k++)
{
for (int l = 0; l < DIM3; l++)
{
sumOf += myHugeArray[i, j, k, l];
}
}
}

}

consoleStopwatch.Report("{0}", sumOf);
}

Here's my first try:

class ConsoleStopwatch : System.Diagnostics.Stopwatch, IDisposable
{
private string message = String.Empty;
bool messageDisplayed = false;

public ConsoleStopwatch()
{
base.Start();
}

public ConsoleStopwatch(string message)
{
this.message = message;

base.Start();
}

private void InternalReport(string report)
{
Console.WriteLine("[{0}] {1} {2}", base.Elapsed, message, report);
}

public void Report(string format, params object[] args)
{
InternalReport(String.Format(format, args));

this.messageDisplayed = true;
}

#region IDisposable Members

public void Dispose()
{
base.Stop();

if (!this.messageDisplayed)
{
InternalReport("");
}
}

#endregion
}

Didn't quite like the implementation. Hope to post it right here after some serious refactoring.

No comments:

Development Catharsis :: Copyright 2006 Mário Romano