Click here to Skip to main content
13,356,318 members (59,564 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 8 Jun 2007

Simple Time-profiling in .NET

, 8 Jun 2007
Rate this:
Please Sign up or sign in to vote.
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

A. Bioli
Software Developer (Senior)
Italy Italy
Andrea Bioli is CEO of Frog Software s.r.l, Italy-based software consulting company specialized in Microsoft technlogies.

You may also be interested in...

Comments and Discussions

GeneralWhy not an 'enter' and 'exit' to increase flexibility Pin
robvon15-Jun-07 15:50
memberrobvon15-Jun-07 15:50 
GeneralRe: Why not an 'enter' and 'exit' to increase flexibility Pin
A. Bioli24-Jun-07 23:37
memberA. Bioli24-Jun-07 23:37 
GeneralSource code problem solved! Pin
A. Bioli12-Jun-07 0:12
memberA. Bioli12-Jun-07 0:12 
QuestionSource code still missing Pin
lvTH11-Jun-07 23:46
memberlvTH11-Jun-07 23:46 
GeneralSource code! Pin
andreabioli11-Jun-07 3:37
memberandreabioli11-Jun-07 3:37 
QuestionIs this active even in release mode? Pin
Marc Clifton11-Jun-07 3:05
protectorMarc Clifton11-Jun-07 3:05 
AnswerRe: Is this active even in release mode? Pin
andreabioli11-Jun-07 3:33
memberandreabioli11-Jun-07 3:33 
GeneralProfiling Pin
balazs_hideghety11-Jun-07 1:57
memberbalazs_hideghety11-Jun-07 1:57 
GeneralRe: Profiling Pin
andreabioli11-Jun-07 3:35
memberandreabioli11-Jun-07 3:35 
Yes, there are commercial profilers, even free profilers, but what sometimes is needed is a solution that you can use for example in production code, or in an application some of your customers are running elsewhere, and so on. A simple solution like this can be fast to setup in situations like these. Do you see your customers to install and use a profiler? Smile | :)
Andrea Bioli
GeneralRe: Profiling Pin
balazs_hideghety11-Jun-07 4:02
memberbalazs_hideghety11-Jun-07 4:02 
GeneralRe: Profiling Pin
andreabioli11-Jun-07 5:29
memberandreabioli11-Jun-07 5:29 
GeneralMissing Code Pin
dgauerke8-Jun-07 6:25
memberdgauerke8-Jun-07 6:25 
GeneralRe: Missing Code Pin
andreabioli8-Jun-07 9:52
memberandreabioli8-Jun-07 9:52 
GeneralRe: Missing Code [modified] Pin
kalyankrishna110-Jun-07 23:01
memberkalyankrishna110-Jun-07 23:01 
GeneralRe: Missing Code Pin
andreabioli10-Jun-07 23:08
memberandreabioli10-Jun-07 23: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 8 Jun 2007
Article Copyright 2007 by A. Bioli
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid