用c++实现获取程序运行的时间
看代码:
1 #include<iostream> 2 #include<ctime> 3 using namespace std; 4 int main() 5 { 6 int i; 7 time_t begin,end; 8 begin=clock(); 9 cout<<"**************************\n\n\n\n\n\nI love you!\n\n\n\n\n**************************"<<endl; 10 i = 0; 11 while(i++ < 1000000000) ; 12 end=clock(); 13 cout<<"runtime:"<<double(end-begin)/CLOCKS_PER_SEC<<"s"<<endl; 14 15 begin = time(NULL); 16 cout<<"**************************\n\n\n\n\n\nI love you!\n\n\n\n\n**************************"<<endl; 17 i = 0; 18 while(i++ < 1000000000) ; 19 end = time(NULL); 20 cout << difftime(end, begin) << 's' << endl; 21 }