time()
可精确到秒.
time(NULL);获取当前的系统时间,返回的结果是一个整数,其值表示从1970年1月1日00:00:00到当前时刻的秒数.
需要的头文件:
#include <time.h>
示例代码:
time_t tmStartTime;
time_t tmEndTime;
tmStartTime = time(NULL);
// do something...
for (int i = 0; i < 10000; i++)
{
printf("%d\n", i);
}
tmEndTime = time(NULL);
printf("Use Time: %ld", tmEndTime - tmStartTime);
QueryPerformanceCounter()
头文件:
#include <windows.h>
用法举例:
LARGE_INTEGER timeStartCount;
LARGE_INTEGER timeEndCount;
LARGE_INTEGER timeFreq;
QueryPerformanceFrequency(&timeFreq);
QueryPerformanceCounter(&timeStartCount);
int nSleepCount = 1234;
Sleep(nSleepCount);
QueryPerformanceCounter(&timeEndCount);
double elapsed = (((double)(timeEndCount.QuadPart - timeStartCount.QuadPart) * 1000/ timeFreq.QuadPart));
cout << "nSleepCount: "<< nSleepCount<<"; 实际用时 : " << elapsed <<"ms"<<endl;
运行结果: