Detection code run Efficiency GetTickCount () use

Source: Internet
Author: User
When debugging a program often want to know the efficiency of the program you write, that is, due to the time that the program runs, I introduce this function to complete this function, she is: GetTickCount () The following is explained on MSDN: GetTickCountThe GetTickCountfunction retrieves the number of milliseconds that have elapsed since the system is started. It is limited to the resolution of the system timer. To obtain the system timer resolution and use the getsystemtimeadjustmentfunction.
DWORD GetTickCount (VOID);
Parameters This function has no parameters. return Values

The return value is the number of milliseconds this have elapsed since the system was started. remarks

The elapsed is stored as a DWORD value. Therefore, the time would wrap around to zero if the system was run continuously for 49.7 days.

If you are need a higher resolution timer, use a multimedia timer or a high-resolution timer.

Windows nt/2000/xp: To obtain the time elapsed since the computer is started, retrieve the System up time counter in the performance data in The registry key Hkey_performance_data. The value returned is a 8-byte value. For more information, performance monitoring. Example Code

The following example demonstrates to handle timer wrap around.

DWORD Dwstart = GetTickCount ();

Stop If this has taken too long
if (GetTickCount ()-Dwstart >=)
    Cancel ();
This function is to get the program to run the current time, so in the detection code before and after the function of the operation, two times the difference is the program to run the approximate time. For example:
DWORD Dwstart;
DWORD Dwend;
Dwstart = GetTickCount ();
The code to detect
Dwend = GetTickCount ();
CString s;
S.format ("%d", dwend-dwstart);
AfxMessageBox (s);
This function can also be used as a timing, for example:

#i nclude <windows.h>

#i nclude <stdio.h>

void Main ()

{

DWORD Dwlast;

DWORD dwcurrent;

DWORD dwinterval = 1000;

Dwlast = GetTickCount ();

int i = 0;

while (true)

{

Dwcurrent = GetTickCount ();

if (Dwcurrent-dwlast < Dwinterval)

Continue

Your code to was executed when interval is elapsed

printf ("dwlast,dwcurrent,diff:%d,%d,%d/n", dwlast,dwcurrent,dwcurrent-dwlast);

Your code to determine

if (i > Ten) break;

i++;

Dwlast = dwcurrent;

}

GetChar ();

Return

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.