-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCodeStopwatch.cs
33 lines (28 loc) · 943 Bytes
/
CodeStopwatch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Diagnostics;
namespace StopwatchTimer
{
public class CodeStopWatch : IDisposable
{
private readonly string _codeUnderTime;
private readonly Action<string> _logAction;
private readonly Stopwatch _codeStopwatch;
public CodeStopWatch(string codeUnderTime) : this(codeUnderTime, Console.WriteLine)
{
}
public CodeStopWatch(string codeUnderTime, Action<string> logAction)
{
_codeUnderTime = codeUnderTime;
_logAction = logAction;
_codeStopwatch = new Stopwatch();
logAction($"Starting {_codeUnderTime}");
_codeStopwatch.Start();
}
public void Dispose()
{
_codeStopwatch.Stop();
_logAction($"Finished {_codeUnderTime} in {_codeStopwatch.Elapsed}");
_codeStopwatch.Reset();
}
}
}