Method 1: Use Stopwatch
Code
Class Program
{
Static void Main (string [] args)
{
Stopwatch sw = new Stopwatch ();
Sw. Reset ();
Sw. Start ();
For (int I = 0; I <11031400; I ++)
{
InternalMethod ();
}
Sw. Stop ();
Console. WriteLine (sw. ElapsedMilliseconds. ToString ());
}
Private static void InternalMethod ()
{
}
}
The above code is heavy
The method is used more than 1 million times, and the time spent is about 7 ms. If you remove the method calls in for, it takes about 3 ms, that is to say, more than 1 million calls take about 5 ms.
For (int I = 0; I <1103140; I ++)
{
// InternalMethod ();
} If you increase for to 0.1 billion times, more than 500 MS is required.
Method 2: Environment. TickCount
Code
Class Program
{
Static void Main (string [] args)
{
Long vTickCount = Environment. TickCount; // The number of milliseconds after the operating system starts, that is, the start time.
For (int I = 0; I <1103140; I ++)
{
// InternalMethod ();
}
Console. WriteLine (Environment. TickCount-vTickCount );
}
}
Method 3: High-Performance precise measurement: Win32 API uses QueryPerformanceCounter () and QueryPerformanceFrequency () methods to support high-precision timing.
These methods are much more accurate than standard millisecond-precision timing methods such as GetTickCount. On the other hand, using an "unmanaged" API function in C # may incur some overhead, but it is much better than using a GetTickCount () API function that is not precise at all.
The first function, QueryPerformanceCounter (), queries the actual value of a high-precision counter at any time. The second function QueryPerformanceFrequency () returns the Count value of the high-precision counter per second. To get the time of a code segment, you need to get the actual value of the high-precision counter at both the beginning and end of the code segment. The difference between the two values indicates the time taken to execute the code snippet.
Then, by dividing the difference by the Count value per second (high-precision timer frequency), you can calculate the elapsed time.
Duration = (stop-start)/frequency
Elapsed time = (stop time-Start Time)/frequency
For more information about QueryPerformanceCounter and QueryPerformanceFrequency, see the MSDN documentation.
The following classes implement the functions of the QueryPerformanceCounter () and QueryPerformanceFrequency () API functions.
Code
Using System;
Using System. Runtime. InteropServices;
Using System. ComponentModel;
Using System. Threading;
Namespace Win32
{
Internal class HiPerfTimer
{
[DllImport ("Kernel32.dll")]
Private static extern bool QueryPerformanceCounter (
Out long lpPerformanceCount );
[DllImport ("Kernel32.dll")]
Private static extern bool QueryPerformanceFrequency (
Out long lpFrequency );
Private long startTime, stopTime;
Private long freq;
// Constructor
Public HiPerfTimer ()
{
StartTime = 0;
StopTime = 0;
If (QueryPerformanceFrequency (out freq) = false)
{
// High-performance counters are not supported
Throw new Win32Exception ();
}
}
// Start Timer
Public void Start ()
{
// Wait for the thread to work
Thread. Sleep (0 );
QueryPerformanceCounter (out startTime );
}
// Stop the timer
Public void Stop ()
{
QueryPerformanceCounter (out stopTime );
}
// Returns the timer time (unit: seconds)
Public double Duration
{
Get
{
Return (double) (stopTime-startTime)/(double) freq;
}
}
}
}
It is easy to use this class. You only need to create a HiPerfTimer instance, and then call Start () to Start timing, Stop () to Stop timing. To obtain the elapsed time, call the Duration () function.
See the following example.
HiPerfTimer pt = new HiPerfTimer (); // create a new HiPerfTimer object
Pt. Start (); // Start the timer
Console. WriteLine ("Test"); // code for timing
Pt. Stop (); & nb