windows 如何显示毫秒

2025-07-16 01:16:53

1、感觉精确到毫秒的当前系统时间是没有意义的,系统当前时间本身就不精确,理由如下:当前的系统时间是由WM的使用者设置的,谁也不会设置精确到毫秒,就算设置的时间与标准时间差了很多,WM本身也无法纠正。要是精确到毫秒计时,那GetTickCount就可以了。

windows 如何显示毫秒

2、使用clock()函数,获取毫秒级(ms)时间[1]#include <time.h>//clock()头文件clock_t start = clock();{statement section}//测试代码段clock_t end = clock();printf("the running time is :%fs\n", (double)(end -start)/(double)CLOCKS_PER_SEC); //秒

windows 如何显示毫秒

3、基于CPU级的方法,获取微秒级(us)时间[2,3]#include <stdio.h>#include <windows.h>//头文件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 :"<<dt*1000<<"ms"<<endl;//dt结果乘以1000,将显示时间调整到ms级别显示,乘以1000000,将显示时间调整到us级别显示

windows 如何显示毫秒

4、备注:可以在代码段添加Sleep()函数测试,如下两个测试实例。例1:500mstime_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

windows 如何显示毫秒

5、例: 5ms测试LARG苇质缵爨E_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 : "<<dt*1000000<<"us"<<endl;运行结果,如下:Running time : 5162.22us

windows 如何显示毫秒

6、通过这个步骤就可以使windows系统精确到毫秒计时了。

windows 如何显示毫秒
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢