Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple Time-profiling in .NET

0.00/5 (No votes)
8 Jun 2007 2  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here