Skip to content

Instantly share code, notes, and snippets.

@ericvoid
Created May 11, 2018 23:09
Show Gist options
  • Save ericvoid/20d26de1d410c93211e28b256fa356bd to your computer and use it in GitHub Desktop.
Save ericvoid/20d26de1d410c93211e28b256fa356bd to your computer and use it in GitHub Desktop.
Simple C# Profiler
public class MyProfiler
{
public int Calls
{
get;
private set;
}
public TimeSpan TotalTime
{
get;
private set;
}
public T Profile<T>(Func<T> f)
{
T result;
Calls++;
var stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
result = f();
stopwatch.Stop();
TotalTime += stopwatch.Elapsed;
return result;
}
public void Profile(Action f)
{
Calls++;
var stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
f();
stopwatch.Stop();
TotalTime += stopwatch.Elapsed;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment