I'm posting a little library. It proved useful in our projects, so I'm sharing it with you. The idea behind it is that you don't always need a full-featured profiler to get important timing information about your running application, at the small cost of injecting the code with these simple calls.
To be really useful, it must be fast and simple to use, and we'll see why in a short time.
The only background information I write here is for the presentation of the timings (see figure). I make use of Advanced TreeView for .NET, that I found to be a very good piece of software (so credits to Andrey Gliznetsov for his good work!). We have already used this control in other situations, and it's very flexible.
Using the Code
Sample code is self-explanatory, but I'll introduce the main concepts here.
First of all, to be useful, the library must be simple and fast to use. Here is the first example:
This will produce an entry in the timings result window, called 'First example', with the time needed to execute the inner code block (can you guess how much it is?). Things get interesting when we want to build a hierarchy of timings: to make another simple example, we write code in a single function, but it can be naturally spread among different functions.
for (int j = 0; j < 10; j++)
Chrono.Start("Second example;Inner calls");
Chrono.Stop("Second example;Inner calls");
To make a hierarchy of timings, you must use the character '
;' to build the name string. In this case, timer "Inner calls" is a subnode of node "Second example". As you can see, a closed timer can be reopened very simply. The system will keep the number of times the timer has been called, calculating the average call time.
With only these two concepts, you can build every complex hierarchy of timers you can need to understand the timing behavior of your application.
Finally, the simple call to view the results:
It will show a window with a multicolumn treeview freely browsable with all the information about your timers (see figure for a simple example). Each timer is shown in the correct subnode, so you can build 'zones' for your app, like 'Business', 'Database', 'UI', and so on. If you don't create a parent timer for some sub-timer, it will be shown with time '
-1' (example, you only create timer "
a;b", without creating a timer "
Header columns are clickable to get every subgroup sorted along that dimension.
As for now, there is no function for exporting or saving of data: we didn't need them.
Points of Interest
Just as simple as that!
The code is really simple, just to make everyone understand where and how things get done!
- 8th June, 2007: Initial post