@@ -33,22 +33,23 @@ static CascadingCodeTimer()
3333 Thread . CurrentThread . Priority = ThreadPriority . Highest ;
3434 }
3535
36- public IDisposable Measure ( string name )
36+ /// <inheritdoc />
37+ public IDisposable Measure ( string name , bool excludeInRelativeCost = false )
3738 {
38- MeasureScope childScope = CreateChildScope ( name ) ;
39+ MeasureScope childScope = CreateChildScope ( name , excludeInRelativeCost ) ;
3940 _activeScopeStack . Push ( childScope ) ;
4041
4142 return childScope ;
4243 }
4344
44- private MeasureScope CreateChildScope ( string name )
45+ private MeasureScope CreateChildScope ( string name , bool excludeInRelativeCost )
4546 {
4647 if ( _activeScopeStack . TryPeek ( out MeasureScope topScope ) )
4748 {
48- return topScope . SpawnChild ( this , name ) ;
49+ return topScope . SpawnChild ( this , name , excludeInRelativeCost ) ;
4950 }
5051
51- return new MeasureScope ( this , name ) ;
52+ return new MeasureScope ( this , name , excludeInRelativeCost ) ;
5253 }
5354
5455 private void Close ( MeasureScope scope )
@@ -66,6 +67,7 @@ private void Close(MeasureScope scope)
6667 }
6768 }
6869
70+ /// <inheritdoc />
6971 public string GetResult ( )
7072 {
7173 var builder = new StringBuilder ( ) ;
@@ -99,14 +101,16 @@ private sealed class MeasureScope : IDisposable
99101 {
100102 private readonly CascadingCodeTimer _owner ;
101103 private readonly IList < MeasureScope > _children = new List < MeasureScope > ( ) ;
104+ private readonly bool _excludeInRelativeCost ;
102105 private readonly TimeSpan _startedAt ;
103106 private TimeSpan ? _stoppedAt ;
104107
105108 public string Name { get ; }
106109
107- public MeasureScope ( CascadingCodeTimer owner , string name )
110+ public MeasureScope ( CascadingCodeTimer owner , string name , bool excludeInRelativeCost )
108111 {
109112 _owner = owner ;
113+ _excludeInRelativeCost = excludeInRelativeCost ;
110114 Name = name ;
111115
112116 EnsureRunning ( ) ;
@@ -121,9 +125,9 @@ private void EnsureRunning()
121125 }
122126 }
123127
124- public MeasureScope SpawnChild ( CascadingCodeTimer owner , string name )
128+ public MeasureScope SpawnChild ( CascadingCodeTimer owner , string name , bool excludeInRelativeCost )
125129 {
126- var childScope = new MeasureScope ( owner , name ) ;
130+ var childScope = new MeasureScope ( owner , name , excludeInRelativeCost ) ;
127131 _children . Add ( childScope ) ;
128132 return childScope ;
129133 }
@@ -151,23 +155,47 @@ private TimeSpan GetElapsedInChildren()
151155 return elapsedInChildren ;
152156 }
153157
158+ private TimeSpan GetSkippedInTotal ( )
159+ {
160+ TimeSpan skippedInSelf = _excludeInRelativeCost ? GetElapsedInSelf ( ) : TimeSpan . Zero ;
161+ TimeSpan skippedInChildren = GetSkippedInChildren ( ) ;
162+
163+ return skippedInSelf + skippedInChildren ;
164+ }
165+
166+ private TimeSpan GetSkippedInChildren ( )
167+ {
168+ TimeSpan skippedInChildren = TimeSpan . Zero ;
169+
170+ foreach ( MeasureScope childScope in _children )
171+ {
172+ skippedInChildren += childScope . GetSkippedInTotal ( ) ;
173+ }
174+
175+ return skippedInChildren ;
176+ }
177+
154178 public void WriteResult ( StringBuilder builder , int indent )
155179 {
156- TimeSpan timeElapsedGlobal = GetElapsedInTotal ( ) ;
180+ TimeSpan timeElapsedGlobal = GetElapsedInTotal ( ) - GetSkippedInTotal ( ) ;
157181 WriteResult ( builder , indent , timeElapsedGlobal ) ;
158182 }
159183
160184 private void WriteResult ( StringBuilder builder , int indent , TimeSpan timeElapsedGlobal )
161185 {
162186 TimeSpan timeElapsedInSelf = GetElapsedInSelf ( ) ;
163- double scaleElapsedInSelf = timeElapsedInSelf / timeElapsedGlobal ;
187+ double scaleElapsedInSelf = timeElapsedGlobal != TimeSpan . Zero ? timeElapsedInSelf / timeElapsedGlobal : 0 ;
164188
165189 WriteIndent ( builder , indent ) ;
166190 builder . Append ( Name ) ;
167191 builder . Append ( ": " ) ;
168192 builder . Append ( timeElapsedInSelf . ToString ( "G" , CultureInfo . InvariantCulture ) ) ;
169- builder . Append ( " - " ) ;
170- builder . Append ( scaleElapsedInSelf . ToString ( "P" , CultureInfo . InvariantCulture ) ) ;
193+
194+ if ( ! _excludeInRelativeCost )
195+ {
196+ builder . Append ( " - " ) ;
197+ builder . Append ( scaleElapsedInSelf . ToString ( "P" , CultureInfo . InvariantCulture ) ) ;
198+ }
171199
172200 if ( _stoppedAt == null )
173201 {
0 commit comments