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
- http://msdn.microsoft.com/en-us/library/ms644904(VS.85).aspx
- http://msdn.microsoft.com/en-us/library/ms644905.aspx
Download Source Code