C++版
#include<iostream>
#include<time.h>
int main()
{
clock_t start_time=clock();
{
//被测试代码
}
clock_t end_time=clock();
cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl; //CLOCKS_PER_SEC表示一秒钟内CPU运行的时钟周期数,用于将clock()函数的结果转化为以秒为单位的量
//输出运行时间,如果是秒为单位,就去掉*1000;如果以微秒为单位,就改为CLOCKS_PER_SEC*1000000
return 0;
}
C语言版
#include<stdio.h>
#include<time.h>
int main()
{
double start=clock();
{
//被测试代码
}
double end=clock();
printf( "\nRunning time is: ");
printf("%fms",(double)(end-start)/CLOCKS_PER_SEC*1000);
return 0;
}