Click here to Skip to main content
14,297,293 members

Simple Time-profiling in .NET

Rate this:
3.80 (4 votes)
Please Sign up or sign in to vote.
3.80 (4 votes)
8 Jun 2007CPOL
With this small library, it's simple to add calls to time-profile your .NET application
Screenshot - ChronoResults.jpg


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:

Chrono.Start("First example");
Chrono.Stop("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.

Chrono.Start("Second example");
for (int j = 0; j < 10; j++) 
         Chrono.Start("Second example;Inner calls");
         Chrono.Stop("Second example;Inner calls");
Chrono.Stop("Second example"); 

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 "a").

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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Comments and Discussions

GeneralWhy not an 'enter' and 'exit' to increase flexibility Pin
robvon15-Jun-07 14:50
memberrobvon15-Jun-07 14:50 
GeneralRe: Why not an 'enter' and 'exit' to increase flexibility Pin
A. Bioli24-Jun-07 22:37
memberA. Bioli24-Jun-07 22:37 
GeneralSource code problem solved! Pin
A. Bioli11-Jun-07 23:12
memberA. Bioli11-Jun-07 23:12 
QuestionSource code still missing Pin
lvTH11-Jun-07 22:46
memberlvTH11-Jun-07 22:46 
GeneralSource code! Pin
A. Bioli11-Jun-07 2:37
memberA. Bioli11-Jun-07 2:37 
QuestionIs this active even in release mode? Pin
Marc Clifton11-Jun-07 2:05
protectorMarc Clifton11-Jun-07 2:05 
AnswerRe: Is this active even in release mode? Pin
A. Bioli11-Jun-07 2:33
memberA. Bioli11-Jun-07 2:33 
GeneralProfiling Pin
balazs_hideghety11-Jun-07 0:57
memberbalazs_hideghety11-Jun-07 0:57 
GeneralRe: Profiling Pin
A. Bioli11-Jun-07 2:35
memberA. Bioli11-Jun-07 2:35 
GeneralRe: Profiling Pin
balazs_hideghety11-Jun-07 3:02
memberbalazs_hideghety11-Jun-07 3:02 
GeneralRe: Profiling Pin
A. Bioli11-Jun-07 4:29
memberA. Bioli11-Jun-07 4:29 
GeneralMissing Code Pin
dgauerke8-Jun-07 5:25
memberdgauerke8-Jun-07 5:25 
GeneralRe: Missing Code Pin
A. Bioli8-Jun-07 8:52
memberA. Bioli8-Jun-07 8:52 
GeneralRe: Missing Code [modified] Pin
kalyankrishna110-Jun-07 22:01
memberkalyankrishna110-Jun-07 22:01 
GeneralRe: Missing Code Pin
A. Bioli10-Jun-07 22:08
memberA. Bioli10-Jun-07 22:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 8 Jun 2007


23 bookmarked