为了解某个代码模块的运行时间,需要为改代码模块打上时间戳。具体步骤如下:
包含头文件:
# include<time.h>
实例如下:
#include <iostream>
#include <time.h>
usingnamespace std;
int main()
{
clock_t start = clock();
// Place your codes here...
clock_t ends = clock();
cout <<"Running Time : "<<(double)(ends - start)/ CLOCKS_PER_SEC << endl;
return0;
}
//clock()函数计算出来的是硬件滴答的数目(时钟周期),不是毫秒。要正确输出时间差需要把它换成秒,
//因此需要除以CLOCKS_PER_SEC。CLOCKS_PER_SEC 表示一秒钟内CPU运行的时钟周期数,用于将clock()
//函数的结果转化为以秒为单位的量,但是这个量的具体值是与操作系统相关的。
running time 的结果以秒为单位,因此若要显示毫秒级别,((double)(ends - start)/ CLOCKS_PER_SEC)*1000即可。