gettickcount()

 

  
  
  
  
  1. #include "stdafx.h"  
  2. #include<windows.h> 
  3.  #include<iostream> 
  4.   int lowsetOne(int N)  
  5. {  
  6.  
  7.     DWORD32 start =GetTickCount();  
  8.     int Ret=0;  
  9.     while(N)  
  10.     {  
  11.         N>>=1;  
  12.         Ret+=N;  
  13.       
  14.     }  
  15.       
  16.     DWORD32 endtime=GetTickCount();  
  17.     std::cout<<endtime-start<<'\n';  
  18.     return Ret;  
  19. }  
  20. int _tmain(int argc, _TCHAR* argv[])  
  21. {  
  22.  
  23.     std::cout<<lowsetOne(10000000000000);  
  24.     return 0;  
  25.  

获得程序运行时间,gettickcount(); 这个函数功能是获得 N!中二进制表示中最低位1的位置,函数获得的值+1就是位置值

For Release configurations, this function returns the number of milliseconds since the device booted, excluding any time that the system was suspended.GetTickCount starts at zero on boot and then counts up from there.

For debug configurations, 180 seconds is subtracted from the the number of milliseconds since the device booted. This enables code that uses GetTickCount to be easily tested for correct overflow handling.

Syntax
 
DWORD GetTickCount(void);
Parameters

None

Return Value

The number of milliseconds indicates success.

Remarks

The resolution of the system timer is based on the OEM's setting. Check with the OEM for details.

The elapsed time is stored as a DWORD value. Therefore, the time will rollover to zero if the system is run continuously for 49.7 days. For Debug configurations, 180 seconds is subtracted to check for overflow conditions in code that relies on GetTickCount. If this code started within 3 minutes of the device booting, it will experience an overflow condition if it runs for a certain amount of time.

Aa915056.note(en-us,MSDN.10).gifNote:
You should not use GetTickCount for drift sensitive applications.

When using GetTickCount, subtraction is safe, even if the rollover occurred, and subtraction always yields the correct difference and the number of clock ticks passed between two tick values. Comparing tick values directly does not always yield the correct results; only compare the differences. Be sure that your code can service the difference before the second rollover, that is, before another 49.7 days pass. Comparisons such as the following are not safe.

 
#define DELTA_TICKS sample_tick_value

// initialized somewhere in the code
DWORD dwStartTick = GetTickCount();
DWORD dwEndTick =   GetTickCount() + DELTA_TICKS;

// The following function fails on a rollover.
BOOL    no_compare_tick_difference()
{
  if ( GetTickCount() > dwEndTick )
    return ( TRUE);
  return (FALSE);
}

The following code shows how to properly use GetTickCount by comparing tick differences. This code handles the rollover situation.

 
BOOL compare_tick_difference()
{
  if ( (GetTickCount() – dwStartTick) > DELTA_TICKS)
    return ( TRUE);
  return (FALSE);
}

You can use the GetTickCount function to time the duration of an activity as shown in the example below.

 
dwOldTime = GetTickCount();
DoSomething();
dwTimeElapsed = GetTickCount() – dwOldTime;

On the STMicro platform, the time returned by GetTickCount includes a ~0.014% time drift, which is by design. This translates to a time lag of approximately 1 second every 2 hours.

你可能感兴趣的:(系统运行时间)