WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 58 users


WorldofASP.NET >> Csharp Programming >> Unedited Csharp

High Performance Counter class in c#

a high performance counter class in c# using P/Invoke
Published Date : 02 Apr 2009
Author : Shailendra Sason
Language : C#,C++
Platform : .NET
Technology : Visual Studio
Views : 1945
Rating : (0 votes so far)



Introduction

Many times while developing some program we require a function that is able to calculate the time elapsed during a certain operation. Below I have defined a class that

calculates the time elapsed for the period very accurately (about 10-14 seconds)  using the functions provided by the win32 library. The win32 functions are called in the c# code

using the p/Invoke mechanism .A detailed explanation of the mechanism is outside the scope of this article.

Main

Below is the code for creating the class along with its description

Since we are using the win32 library functions via platform invoke services (P/Invoke) hence we need to include the library which provides support for the same in c#

 

using System.Runtime.InteropServices; "color: blue;">""><o:p>

 

Now we would be adding the functions for calculating the time.

 

 
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(
out long lpPerformanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(
out long lpFrequency); 

The DllImport attribute defines that the function that is provided in an unmanaged class defined in the braces (Kernel32.dll in our case) .Next are the two functions that we would be using

The QueryPerformanceFrequency function provides the frequency of the system performance counter and the QueryPerformanceCounter provides the current value of the counter at that instant.The values stored in the parameters that are defined as “out”

Now we have the two values we can very well calculate the elapsed time as

 

QueryPerformanceFrequency(out startTime);
…………………………..
………………………
………………………
QueryPerformanceCounter(out stopTime);
duration = (double)(stopTime - startTime) / (double)freq;
timeElapsed = duration; 


The function is called at the start and the end ,the values are noted and from there the time is calculated .The startTime and stopTime are infact counts hence we need to divide them by the frequency to calculate the final time elapsed.

 
The counter class is defined below .I consists of two public functions and one public variable .The functions start() and stop () are used to at the beginning and end of the code

For which we want to calculate the durartion .The user can get the final value via the

variable timeElapsed

 

using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;using System.Diagnostics;namespace HighPerformanceCounter
{
class counter
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(
out long lpPerformanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(
out long lpFrequency);long startTime, stopTime;double duration;long freq;public double timeElapsed;public counter()
{
QueryPerformanceFrequency(out freq);
}
public void start()
{
QueryPerformanceFrequency(out startTime);
}
public void stop()
{
QueryPerformanceCounter(out stopTime);
duration = (double)(stopTime - startTime) / (double)freq;
timeElapsed = duration;
}
}
}

                                                  

  A screenshot of the application showing the time elapsed in milliseconds

Conclusion

Thus we can use the code to calculate the time elapsed with a high precsion.The code attached provides the sample application’s code with

The counter class.

References

Include all the useful links or references that can help users learn about your tutorial

  1. http://msdn.microsoft.com/en-us/library/ms644904(VS.85).aspx
  2. http://msdn.microsoft.com/en-us/library/ms644905.aspx

Download Source Code

 




Other Related and Popular Articles :

Value Type and Reference Type in C
It will give a clear idea about value type and reference type in c#. Thus help to readers to use proper data type in coding in their application.

CaptchaImage
CaptchaImage

c# and Truncate Log SQL Server 2008 without changing Database Recovery Model
c# and Truncate Log SQL Server 2008 without changing Database Recovery Model

Serialization - Serializing Objects (Part One)
This article mainly describe how to use BinaryFormatter and SoapFormatter to serialize and deserialize objects

XStream for .NET
how XStream .NET serialize and deserialize objects

264
264


Author Profile : Shailendra Sason

Click here to view Author Profile


How would you rate the quality of this content?
Poor Excellent

Comments

Leave New Comments


Article Content copyright by Shailendra Sason
Everything else Copyright © by WorldofASP.NET 2010

Category
.NET 3.5
AJAX and ATLAS
ASP.NET
C# Programming
Classic ASP
Enterprise Systems
General .NET
VB.NET Programming
Announcements
Earn Cash by writing an article or review
For more info Click here







Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
© 2002-2010 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | ASP Hosting | ASP.NET Hosting | Phone Card | Calling Card |Stock Investing