C++如何计算代码运行时间

#include<iostream>
#include<ctime>
using namespace std;
void main()
{
	//统计运行时间精确到秒
	time_t tstart, tend;
	long i=1000000000000;
	tstart=time(0);
	while(i--);
	tend=time(0);
	cout<<difftime(tend,tstart)<<endl;
	//统计运行时间精确到毫秒
	i=1000000000000;
	double tstart1, tend1;
	tstart1 = clock();
	while(i--);
	tend1 = clock();
	cout<<(tend1-tstart1)/CLOCKS_PER_SEC<<endl;
}

 

话不多说,直接看代码和运行结果截图。



 

你可能感兴趣的:(C++)