Skip to content

Instantly share code, notes, and snippets.

@jonsagara
Last active September 21, 2016 21:14
Show Gist options
  • Save jonsagara/fcb28b137835eab2e3eb0d2d254e8415 to your computer and use it in GitHub Desktop.
Save jonsagara/fcb28b137835eab2e3eb0d2d254e8415 to your computer and use it in GitHub Desktop.
Quick-and-dirty C# profiler. Really, it's just a wrapper for Stopwatch. Replace Console.WriteLine with whatever output makes sense.
public class QuickProfiler : IDisposable
{
private readonly Stopwatch _sw = Stopwatch.StartNew();
private readonly string _name;
public QuickProfiler(string name)
{
_name = name;
}
public void Dispose()
{
_sw.Stop();
Console.WriteLine($"QuickProfiler: {_name} took {_sw.ElapsedMilliseconds}ms");
}
}
@jonsagara
Copy link
Author

Usage:

using (new QuickProfiler("Some Expensive Operation"))
{
    var result = SomeExpensiveOperation();
}

Sample output:

QuickProfiler: Some Expensive Operation took 330ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment