Click here to Skip to main content
Click here to Skip to main content
Go to top

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

Introduction

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.

Background

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");
Thread.Sleep(100);
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");
         Thread.Sleep(100);
         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:

Chrono.ShowResults();

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!

History

  • 8th June, 2007: Initial post

License

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

Share

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.

Comments and Discussions

 
GeneralWhy not an 'enter' and 'exit' to increase flexibility Pinmemberrobvon15-Jun-07 14:50 
GeneralRe: Why not an 'enter' and 'exit' to increase flexibility PinmemberA. Bioli24-Jun-07 22:37 
GeneralSource code problem solved! PinmemberA. Bioli11-Jun-07 23:12 
QuestionSource code still missing PinmemberlvTH11-Jun-07 22:46 
GeneralSource code! Pinmemberandreabioli11-Jun-07 2:37 
QuestionIs this active even in release mode? PinprotectorMarc Clifton11-Jun-07 2:05 
AnswerRe: Is this active even in release mode? Pinmemberandreabioli11-Jun-07 2:33 
GeneralProfiling Pinmemberbalazs_hideghety11-Jun-07 0:57 
GeneralRe: Profiling Pinmemberandreabioli11-Jun-07 2:35 
GeneralRe: Profiling Pinmemberbalazs_hideghety11-Jun-07 3:02 
GeneralRe: Profiling Pinmemberandreabioli11-Jun-07 4:29 
GeneralMissing Code Pinmemberdgauerke8-Jun-07 5:25 
GeneralRe: Missing Code Pinmemberandreabioli8-Jun-07 8:52 
GeneralRe: Missing Code [modified] Pinmemberkalyankrishna110-Jun-07 22:01 
GeneralRe: Missing Code Pinmemberandreabioli10-Jun-07 22:08 

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

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

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