JavaScript:
var timer = Stopwatch.StartNew();
var ms = timer.ElapsedMilliseconds;
Just 2 Lines and no ugly indenting of my code. There is really no need to stop the Stopwatch. No resources will be freed if you do, it just stops counting.
JavaScript:
using System.Diagnostics;
var timer = new Stopwatch();
timer.Start();
// do something you want to profile
timer.Stop();
var ms = timer.ElapsedMilliseconds;
// write timing to logs
First, here's the usage example of the performance timing logging class that will be listed below:
JavaScript:
using (new PerfTimerLogger("name of code being profiled"))
{
// do something you want to profile
}
Here's an implementation of the PerfTimerLogger class that allows it to be used as the above example demonstrates:
JavaScript:
using System.Diagnostics;
public class PerfTimerLogger : IDisposable
{
public PerfTimerLogger(string message)
{
this._message = message;
this._timer = new Stopwatch();
this._timer.Start();
}
string _message;
Stopwatch _timer;
public void Dispose()
{
this._timer.Stop();
var ms = this._timer.ElapsedMilliseconds;
// log the performance timing with the Logging library of your choice
// Example:
// Logger.Write(
// string.Format("{0} - Elapsed Milliseconds: {1}", this._message, ms)
// );
}
}