Obtain the exact time in C #

Source: Internet
Author: User

It is sometimes necessary to obtain the exact time in the program. It is often used to test the performance of a program and use accurate time timing. Or in other cases, the exact time is required. This requires a function QueryPerformanceCounter (). The usage starts from the first time QueryPerformanceCounter () is called, and the call ends again after a period of time. The difference between the two is divided by the frequency of QueryPerformanceFrequency (), that is, the number of seconds between the start and the end. Because the timing function itself takes a small amount of time, it is generally ignored.
Note that if you need to use this function on a multi-processor computer, you need to specify the called processor. Because different processors have different results.
The following class can implement this function.


Public class QueryPerformance
{
[DllImport ("Kernel32.dll")]
Private static extern bool QueryPerformanceCounter (out long performanceCount );
 
[DllImport ("Kernel32.dll")]
Private static extern bool QueryPerformanceFrequency (out long frequency );
 
Private long begintTime = 0; // start time

Private long endTime = 0; // End Time
 
Private long frequency = 0; // processor frequency

Public long BegintTime
{
Get {return begintTime ;}
}

Public long EndTime
{
Get {return endTime ;}
}

Public long Frequency
{
Get {return frequency ;}
}
 
 
Public QueryPerformance ()
{
QueryPerformanceFrequency (frequency) // obtain the frequency
}
 
Public void Start ()
{
QueryPerformanceCounter (begintTime );
}
 
Public void Stop (bool showRecord)
{
QueryPerformanceCounter (endTime );
 
If (showRecord)
{
Console. WriteLing (string. Format ("Time: {0} s", TastTime ));
}
}
 
Public double TastTime // time consumed: Unit: seconds
{
Get
{
If (frequency> 0)
Return (double) (endTime-begintTime)/frequency;
Else
Return 0;
}
}
}
Public class QueryPerformance
{
[DllImport ("Kernel32.dll")]
Private static extern bool QueryPerformanceCounter (out long performanceCount );

[DllImport ("Kernel32.dll")]
Private static extern bool QueryPerformanceFrequency (out long frequency );

Private long begintTime = 0; // start time

Private long endTime = 0; // End Time

Private long frequency = 0; // processor frequency

Public long BegintTime
{
Get {return begintTime ;}
}

Public long EndTime
{
Get {return endTime ;}
}

Public long Frequency
{
Get {return frequency ;}
}


Public QueryPerformance ()
{
QueryPerformanceFrequency (frequency) // obtain the frequency
}

Public void Start ()
{
QueryPerformanceCounter (begintTime );
}

Public void Stop (bool showRecord)
{
QueryPerformanceCounter (endTime );

If (showRecord)
{
Console. WriteLing (string. Format ("Time: {0} s", TastTime ));
}
}

Public double TastTime // time consumed: Unit: seconds
{
Get
{
If (frequency> 0)
Return (double) (endTime-begintTime)/frequency;
Else
Return 0;
}
}
}
Call:


QueryPerformance queryPerformance = new QueryPerformance ();
 
QueryPerformance. Start ();
 
// Code blocks and functions
 
QueryPerformance. Stop (true );

From Poplar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.