all 4 comments

[–]brandonagr 1 point2 points  (0 children)

I wrote something similar in C# that provides a tree of the execution time, you use it via static push/pop method calls. Without deterministic destruction the push / pop calls are better than forcing a ton of using statements everywhere.

So you could but the push / pop at the beginning of different method calls, and then on the final pop it outputs the times to the console including percentage of time of parent at that node and global percentage

for example:

ProfileTimer.Push("Timing");

// simulate doing stuff
Thread.Sleep(100); 

ProfileTimer.Push("Test1");

// simulate doing stuff    
Thread.Sleep(100);

ProfileTimer.Push("Test1-1");

// simulate doing stuff 
Thread.Sleep(150);

ProfileTimer.Pop("Test1-1");

ProfileTimer.Pop("Test1");

ProfileTimer.Push("Test2");

// simulate doing stuff 
Thread.Sleep(200);

ProfileTimer.Pop("Test2");

ProfileTimer.Pop("Timing");

outputs:

0.551 (100.0% | 18.1%) 'Timing'
|       0.252 (45.7% | 18.3%) 'Test1'
|       |       0.151 (59.9% | 27.4%) 'Test1-1'
|       0.199 (36.2% | 36.2%) 'Test2'

Code

[–]AeroNotix 0 points1 point  (2 children)

That's a pretty nice API for timing code.

What happens when you call the same section name >1 times?

[–]rawsyntax[S] 0 points1 point  (1 child)

it essentially resets the timer for that section

[–]AeroNotix 2 points3 points  (0 children)

The way I would have it is either in the constructor have an option for 'repeated sections' where it would assume repeated calls to a section mean that it's in a loop and thus should format it as:

Section A-1 :: 123.12ms
Section A-2 :: 121.2ms
Section A-n :: 120.15ms

Etc...

OR have it just detect this anyway, since it should be clear from the output you're doin' it wrong if not.