<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
			<Title>class StopwatchAndMemoryLogger</Title>
			<Author>
				Fons Sonnemans
			</Author>
			<Description>
				Log Duration and Memory usage to the Console
			</Description>
			<Shortcut>clsStopwatchAndMemoryLogger</Shortcut>
		</Header>
		<Snippet>
			<Code Language="csharp">
				<![CDATA[// using var sw = StopwatchAndMemoryLogger.StartNew();
				
public sealed class StopwatchAndMemoryLogger : IDisposable {

    private readonly System.Diagnostics.Stopwatch _stopwatch = new System.Diagnostics.Stopwatch();
    private long _initialAllocatedBytes;

    public static StopwatchAndMemoryLogger StartNew() {
        var sw = new StopwatchAndMemoryLogger();
        sw.Start();
        return sw;
    }

    public void Start() {
        _stopwatch.Start();
        _initialAllocatedBytes = GC.GetAllocatedBytesForCurrentThread();
    }

    public void StopAndLog() {
        if (_stopwatch.IsRunning) {
            var allocatedBytes = GC.GetAllocatedBytesForCurrentThread() - _initialAllocatedBytes;
            _stopwatch.Stop();
            System.Console.WriteLine($$"// Duration:  {_stopwatch.Elapsed.TotalSeconds:N5} seconds");
            System.Console.WriteLine($$"// Allocated: {allocatedBytes:N0} bytes");
            System.Console.WriteLine();
        }
    }

    public void Restart() {
        this.StopAndLog();
        _initialAllocatedBytes = GC.GetAllocatedBytesForCurrentThread();
        _stopwatch.Restart();
    }

    public void Dispose() => this.StopAndLog();
}]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>