windows 如何显示毫秒
的有关信息介绍如下:在windows系统当中,有什么办法可以获取到当前系统的时间,如:2009年10月28日19时43分3秒50毫秒也许有人会说可以调用GetLocalTime和GetSystemtime以及GetTickCount函数,但是调用GetLocalTime和GetSystemtime获取的结构中对毫秒始终为0,GetTickCount是获取自系统开机以后所经历的时间,是毫秒数,而不是系统当前时间。
感觉精确到毫秒的当前系统时间是没有意义的,系统当前时间本身就不精确,理由如下:当前的系统时间是由WM的使用者设置的,谁也不会设置精确到毫秒,就算设置的时间与标准时间差了很多,WM本身也无法纠正。要是精确到毫秒计时,那GetTickCount就可以了。
使用clock()函数,获取毫秒级(ms)时间
#include
clock_t start = clock();
{statement section}//测试代码段
clock_t end = clock();
printf("the running time is :%fs\n", (double)(end -start)/(double)CLOCKS_PER_SEC); //秒
基于CPU级的方法,获取微秒级(us)时间[2,3]
#include
LARGE_INTEGER nFreq;
LARGE_INTEGER t1;
LARGE_INTEGER t2;
double dt;
QueryPerformanceFrequency(&nFreq);QueryPerformanceCounter(&t1);
{statementsection}//测试代码段QueryPerformanceCounter(&t2);dt = (t2.QuadPart - t1.QuadPart) / (double)nFreq.QuadPart;cout<<"Running time :"<
备注:可以在代码段添加Sleep()函数测试,如下两个测试实例。
例1:500ms
time_t start = clock(); Sleep(1000);//500mstime_t end = clock();printf("the running time is :%fms\n", (double)(end -start));
运行结果,如下:
the running time is :1001.000000ms
例: 5ms测试
LARGE_INTEGER nFreq,t1,t2;double dt;QueryPerformanceFrequency(&nFreq);QueryPerformanceCounter(&t1);Sleep(5);//5ms,其中1ms=1000usQueryPerformanceCounter(&t2);dt = (t2.QuadPart - t1.QuadPart)/(double)nFreq.QuadPart;cout<<"Running time : "<
运行结果,如下:
Running time : 5162.22us
通过这个步骤就可以使windows系统精确到毫秒计时了。