Last active
April 26, 2023 08:37
-
-
Save b3b00/90baf87166a51b8e85dad922fb35ee45 to your computer and use it in GitHub Desktop.
C# chrono
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Diagnostics; | |
namespace Chrono.Ext | |
{ | |
public class ChronoExts | |
{ | |
public List<long> ElapsedMilliseconds { get; set; } | |
public IDictionary<string,long> LabeledElapsedMilliseconds { get; set; } | |
private Stopwatch chrono; | |
public ChronoExts() | |
{ | |
chrono = new Stopwatch(); | |
ElapsedMilliseconds = new List<long>(); | |
LabeledElapsedMilliseconds = new Dictionary<string, long>(); | |
} | |
public void Start() | |
{ | |
chrono.Start(); | |
} | |
public void Stop(string label = null) | |
{ | |
chrono.Stop(); | |
ElapsedMilliseconds.Add(chrono.ElapsedMilliseconds); | |
if (!string.IsNullOrEmpty(label)) | |
{ | |
LabeledElapsedMilliseconds[label] = chrono.ElapsedMilliseconds; | |
} | |
} | |
public void Tick(string label = null) | |
{ | |
chrono.Stop(); | |
ElapsedMilliseconds.Add(chrono.ElapsedMilliseconds); | |
if (!string.IsNullOrEmpty(label)) | |
{ | |
LabeledElapsedMilliseconds[label] = chrono.ElapsedMilliseconds; | |
} | |
chrono.Reset(); | |
chrono.Start(); | |
} | |
public override string ToString() | |
{ | |
StringBuilder builder = new StringBuilder(); | |
foreach (var step in LabeledElapsedMilliseconds) | |
{ | |
builder.AppendLine($"{step.Key} : {step.Value} ms"); | |
} | |
return builder.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment