#nullable enable using System; using System.Diagnostics; namespace Swan.Diagnostics { public static partial class Benchmark { /// /// Represents a disposable benchmark unit. /// /// private sealed class BenchmarkUnit : IDisposable { private readonly String _identifier; private Boolean _isDisposed; // To detect redundant calls private Stopwatch? _stopwatch = new Stopwatch(); /// /// Initializes a new instance of the class. /// /// The identifier. public BenchmarkUnit(String identifier) { this._identifier = identifier; this._stopwatch?.Start(); } /// public void Dispose() => this.Dispose(true); /// /// Releases unmanaged and - optionally - managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. private void Dispose(Boolean alsoManaged) { if(this._isDisposed) { return; } if(alsoManaged) { Add(this._identifier, this._stopwatch?.Elapsed ?? default); this._stopwatch?.Stop(); } this._stopwatch = null; this._isDisposed = true; } } } }