What I did was create an IDisposable
class that would start a stopwatch in the constructor and stop/print the result in the dispose:
public class Superwatch : IDisposable
{
static Stopwatch Watch = new Stopwatch();
static Superwatch()
{
Watch.Start();
}
TimeSpan Start;
public Superwatch()
{
Start = Watch.Elapsed;
}
public void Dispose()
{
TimeSpan elapsed = Watch.Elapsed - Start;
Console.WriteLine($"Time elapsed: {elapsed}");
}
}
Then just pack the method into a using of an instance of the class you created.
using (var watch = new Superwatch())
{
//piece of code
}
Not as clean as a decorator, but relatively clean imo and configurable for pieces of code.