c++程序记时模板 测试程序运行时间

版权声明:原创文章,转载请注明出处 https://blog.csdn.net/hza419763578/article/details/88603300

有时为了测试程序运行时间,或者程序陷入了死循环,要不定期打印某些变量的值,又懒得打断点,太麻烦,此时可以定期输出程序变量值,观察运行情况

下面给出c++计时模板  前不久为启动和关闭oracle服务写脚本的时候用到了,今天写bfs直接复制过来用了下,发现不错,记录下

#include<iostream>
#include <ctime>
using namespace std;
int main(){
	time_t start ,end;
	double cost;
	time(&start);//开始计时

	int n=1;
	while(true){
		n++;

		time(&end);//结束计时
		cost=difftime(end,start);//时间差
		if(cost>=2){//每隔两秒输出一次n的值
			cout<<"n="<<n<<endl;
			time(&start);//开始计时
		}
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/hza419763578/article/details/88603300