QueryPerformanceCounter()這個函數返回高精確度效能計數器的值,它可以以微妙為單位計時.但是QueryPerformanceCounter()確切的精確計時的最小單位是與系統有關的,所以,必須要查詢系統以得到QueryPerformanceCounter()返回的嘀噠聲的頻率.QueryPerformanceFrequency()提供了這個頻率值,返回每秒嘀噠聲的個數.計算確切的時間是從第一次調用QueryPerformanceCounter()開始的假設得到的LARGE_INTEGER 為nStartCounter,過一段時間後再次調用該函數結束的,設得到nStopCounter.兩者之差除以QueryPerformanceFrequency()的頻率就是開始到結束之間的秒數.由於計時函數本身要耗費很少的時間,要減去一個很少的時間開銷.但一般都把這個開銷忽略.公式如下:
nStopCounter-nStartCounter
ElapsedTime=------------------------------------ - overhead
frequency double time=(nStopCounter.QuadPart-nStartCounter.QuadPart)/frequency.QuadPart
使用QueryPerformanceCounter()可以得到微秒級(1/1000000秒)的時間:
LONGLONG t1,t2;
LONGLONG persecond;
QueryPerformanceFrequency((LARGE_INTEGER *)&persecond);//詢問系統一秒鐘的頻率
QueryPerformanceCounter((LARGE_INTEGER *)&t1);
//下面是你要計算已耗用時間的程式碼
...
QueryPerformanceCounter((LARGE_INTEGER *)&t2);
double time=(t2-t1)/persecond;
這兩個函數需要mmsystem.h,並要串連winmm.lib ==== typedef __int64 LONGLONG;
typedef unsigned __int64 ULONGLONG; #define MAXLONGLONG (0x7fffffffffffffff)
#if defined(MIDL_PASS)
typedef struct _LARGE_INTEGER {
#else // MIDL_PASS
typedef union _LARGE_INTEGER {
struct {
DWORD LowPart;
LONG HighPart;
};
struct {
DWORD LowPart;
LONG HighPart;
} u;
#endif //MIDL_PASS
LONGLONG QuadPart;
} LARGE_INTEGER;
=============== C 中獲得當前系統時間
引用:
//方案— 優點:僅使用C標準庫;缺點:只能精確到秒級
#include <time.h>
#include <stdio.h>
int main( void )
{
time_t t = time( 0 );
char tmp[64];
strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );
puts( tmp );
return 0;
}
引用:
//方案二 優點:能精確到毫秒級;缺點:使用了windows API
#include <windows.h>
#include <stdio.h>
int main( void )
{
SYSTEMTIME sys;
GetLocalTime( &sys );
printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);
return 0;
}
引用:
//方案三,優點:利用系統函數,還能修改系統時間
#include<stdlib.h>
#include<iostream>
using namespace std;
void main()
{
system("time");
}
引用:
//方案四,將目前時間折算為秒級,再通過相應的時間換算即可
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
time_t now_time;
now_time = time(NULL);
cout<<now_time;
return 0;
}
time_t time(time_t *timer);
得到系統時間,
struct tm *gmtime(const time_t *timer;
轉化成tm格式。
struct tm *localtime(const time_t *timer);
有時區校正的。
char *asctime(const struct tm *timeptr);
得到 Wed Jan 02 02:03:55 1980\n\0 標準GSM格式時間字串。
size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
根據格式字串產生字串。
樣本:
char today[32] ="";
char second[32] ="";
tm *temptm;
time_t temptime;
temptime= time(0);
temptm = localtime(&temptime);
sprintf(today,"%d-%02d-%02d",temptm->tm_year+1900,temptm->tm_mon+1,temptm->tm_mday);
sprintf(second,"%02d:%02d:%02d",temptm->tm_hour,temptm->tm_min,temptm->tm_sec);
cout << "Time:" << second << endl;
struct tm *lt;
#ifdef __LINUX_OS
struct timezone tz;
struct timeval now;
//獲得
時間戳記
gettimeofday(&now, &tz);
lt = localtime(&now.tv_sec);
#else
long now;
time(&now);
lt = localtime(&now);
#endif
以上是兩種系統下的時間擷取方法Linux和Windows下的
// 將目前時間轉換成yyy-mm-dd hh:mm:ss的格式
static char buff[30] = {0};
sprintf(buff, "%4d-%02d-%02d %02d:%02d:%02d",
lt->tm_year+1900,
lt->tm_mon+1,
lt->tm_mday,
lt->tm_hour,
lt->tm_min,
lt->tm_sec); |