用高精度计时器
#include <iostream>
#include <windowsh>
using namespace std ;
int main(void)
{
LARGE_INTEGER BegainTime ;
LARGE_INTEGER EndTime ;
LARGE_INTEGER Frequency ;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&BegainTime) ;
//要测试的代码放在这里
QueryPerformanceCounter(&EndTime) ;
cout << ( EndTimeQuadPart - BegainTimeQuadPart )1000 / FrequencyQuadPart ;
system("pause") ;
return 0 ;
}
clock()返回的是CPU时钟计时单元,而CLOCKS_PER_SEC它用来表示一秒钟会有多少个时钟计时单元,所以正确的运行时间是(finish-start)/CLOCKS_PER_SEC,这样就能得到执行了多少秒,要得到毫秒的话再乘以10000,微妙再乘以10000
var
StartTime, EndTime: cardinal;
begin
StartTime := GetTickCount;
sleep(1000);
EndTime := GetTickCount;
caption := (IntToStr(EndTime - StartTime) + ' ms');
end;
#include<stdioh>
#include <sys/timebh>
#include <timeh>
int main(void) //判断经过的时间,毫秒级
{
unsigned long t1,t2,elapsed_time;
unsigned long t11,t21;
int s,ms;
struct timeb timebuffer;
ftime(&timebuffer);
t11=timebuffertime;
t1=timebuffermillitm;
//程序体
ftime(&timebuffer);
t21=timebuffertime;
t2=timebuffermillitm;
elapsed_time =1000(t21-t11)+t2-t1;
s=elapsed_time/1000;
ms=elapsed_time-s1000;
printf("%ds:%dms",s,ms);
return 0;
}
#include "timeh"
#include "stdioh"
main()
{
double start, finish;
start = clock();//取开始时间
printf("Hello, World!\n");
finish = clock();//取结束时间
printf( "%f seconds\n",(finish - start) / CLOCKS_PER_SEC);//以秒为单位显示之
}
上面的代码理论上是可以显示printf("Hello, World!\n");语句的运行时间的,但我猜实际的显示结果是0,因为printf("Hello, World!\n");这个语句的运行时间是可以忽略不计的,加一个次数较多的循环才能看到效果
以上就是关于C++实现计算程序运行时间全部的内容,包括:C++实现计算程序运行时间、C++计算一个程序运行时间,精确到毫秒、delphi怎么计算程序运行时间等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)