{"id":3313,"date":"2020-01-30T15:48:01","date_gmt":"2020-01-30T14:48:01","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=3313"},"modified":"2024-11-12T17:16:03","modified_gmt":"2024-11-12T16:16:03","slug":"c-timeruler","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-timeruler\/","title":{"rendered":"C# TimeRuler"},"content":{"rendered":"\n<p>Measure time elapsed<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">public class TimeRuler\n{\n    private readonly Stopwatch stopWatch = new Stopwatch();\n    private readonly StringBuilder stringBuilder = new StringBuilder();\n    private long _prevMeasure = 0;\n    private string currentDescription;\n\n    public TimeRuler(bool start = true)\n    {\n        if (start) stopWatch.Start();\n    }\n    public void Measure(string description)\n    {\n        var measure = stopWatch.ElapsedMilliseconds;\n        var added = measure - _prevMeasure;\n        _prevMeasure = measure;\n        stringBuilder.AppendLine($\"{measure,12} {added,12} + {description}\");\n    }\n\n    public void Start(string description)\n    {\n        currentDescription = description;\n        Measure($\"Start: {description}\");\n    }\n    public void End() => Measure($\"End: {currentDescription}\");\n    public override string ToString() => stringBuilder.ToString();\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Diagnostics;\nusing System.Text;\n\nnamespace Domain.Helpers\n{\n    public class TimeRuler : IDisposable\n    {\n        private Stopwatch stopWatch = new Stopwatch();\n        private StringBuilder stringBuilder = new StringBuilder();\n        private long _prevMeasure = 0;\n        public TimeRuler(bool start = true)\n        {\n            if (start)\n                stopWatch.Start();\n        }\n        public void Measure(string description)\n        {\n            var measure = stopWatch.ElapsedMilliseconds;\n            var added = measure - _prevMeasure;\n            _prevMeasure = measure;\n            stringBuilder.AppendLine($\"{measure,12} {added,12} + {description}\");\n        }\n        public override string ToString()\n        {\n            return stringBuilder.ToString();\n        }\n        #region IDisposable Support\n        private bool disposedValue = false;\n\n        protected virtual void Dispose(bool disposing)\n        {\n            if (!disposedValue)\n            {\n                if (disposing)\n                {\n                    if (stopWatch.IsRunning)\n                        stopWatch.Stop();\n                    stopWatch = null;\n                }\n                disposedValue = true;\n            }\n        }\n        public void Dispose()\n        {\n            Dispose(true);\n        }\n        #endregion\n    }\n}\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using (var timeRuler = new TimeRuler())\n{\n\ttimeRuler.Measure(\"A\");\n\tCalculate1();\n\ttimeRuler.Measure(\"B\");\n\tCalculate2();\n\ttimeRuler.Measure(\"C\");\n\tConsole.WriteLine(timeRuler.ToString())\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Measure time elapsed<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-3313","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=3313"}],"version-history":[{"count":3,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3313\/revisions"}],"predecessor-version":[{"id":9029,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3313\/revisions\/9029"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=3313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=3313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=3313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}