Skip to content

Instantly share code, notes, and snippets.

@b3b00
Last active April 26, 2023 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b3b00/90baf87166a51b8e85dad922fb35ee45 to your computer and use it in GitHub Desktop.
Save b3b00/90baf87166a51b8e85dad922fb35ee45 to your computer and use it in GitHub Desktop.
C# chrono
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