C++中需要计算算法的运行时间,首先导入头文件
#include<time.h>
实例代码如下,输出结果的单位为秒:
#include<iostream>
#include<string>
#include<time.h>
int main()
{
clock_t start = clock();
for (int i = 10000; i < 10000; i++)
int x = 0;
clock_t end = clock();
auto gap = (double)(end - start) / CLOCKS_PER_SEC;
cout << "起始时间:" << (double)(start)/CLOCKS_PER_SEC << endl;
cout << "结束时间:" << (double)(end)/ CLOCKS_PER_SEC << endl;
cout << "时间间隔为:" << gap << endl;
return 0;
}