Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

StepTimer Class - A Performance Tool - .NET Framework

RSS
Modified on Fri, Dec 11, 2020, 12:41 PM by Administrator Categorized as ┬ĚNet Framework
using System.Diagnostics;
using System.Text;

public class StepTimer
{
    private string _name;
    private Stopwatch _sw;
    private int _stepNum;
    private bool _silent;
    private StringBuilder _sb;
    private long _lastMs;

    public static StepTimer StartNew(string name, bool silent = false)
    {
        return new StepTimer(name, silent);
    }
    private StepTimer(string name, bool silent)
    {
#if DEBUG
        if (!_silent)
        {
            _name = name;
            _stepNum = 1;
            _silent = silent;
            _sb = new StringBuilder();
            _sb.AppendLine($"StepTimer '{_name}'");
            _lastMs = 0;
            _sw = Stopwatch.StartNew();
        }
#endif
    }

    public void EndStep(string label = "")
    {
#if DEBUG
        if (_silent)
            return;

        var ms = _sw.ElapsedMilliseconds - _lastMs;
        _sb.AppendLine($"{_stepNum:00},{ms/1000.0:0.000},{label.Replace(",",";")}");
        _stepNum++;
        _lastMs = _sw.ElapsedMilliseconds;
#endif
    }
    public void Done()
    {
#if DEBUG
        if (_silent)
            return;

        _sw.Stop();

        var doubleLine = "=".PadRight(100, '=');
        Debug.Print(string.Empty);
        Debug.Print(doubleLine);
        Debug.Print(_sb.ToString());
        Debug.Print($"TOTAL ELAPSED TIME = {_sw.ElapsedMilliseconds / 1000.0:0.000}s");
        Debug.Print(doubleLine);
        Debug.Print(string.Empty);
#endif
    }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2021, Patrick Jasinski.