//clock_t #include <iostream> #include <ctime> #include <cmath> void run() { for(int i = 0; i < 100000000; i++) double temp = std::sqrt(i); } int main(int argc, char *argv[]) { //clock clock_t t = clock(); run(); t = clock()-t; //CLOCKS_PER_SEC std::cout<<"Clicks: "<<t<<std::endl; std::cout<<"Time: "<<float(t)/CLOCKS_PER_SEC<<" seconds ..."<<std::endl; return 0; }
//time_t #include <iostream> #include <ctime> #include <cmath> void run() { for(int i = 0; i < 100000000; i++) double temp = std::sqrt(i); } int main(int argc, char *argv[]) { //time time_t t, t2; time(&t);//get the current time run(); time(&t2); //difftime std::cout<<(t2-t)<<" seconds ..."<<std::endl; std::cout<<difftime(t2, t)<<" seconds ..."<<std::endl; return 0; }
//struct tm #include <iostream> #include <ctime> #include <cmath> int main(int argc, char *argv[]) { struct tm m; m.tm_hour = 0; m.tm_min = 0; m.tm_sec = 0; m.tm_year = 100; m.tm_mon = 0; m.tm_mday = 1; time_t t = mktime(&m); //ctime std::cout<<ctime(&t)<<std::endl; return 0; }
//time_t to struct tm #include <iostream> #include <ctime> int main(int argc, char *argv[]) { time_t t; struct tm *timeInfo; //localtime time(&t); timeInfo = localtime(&t); //asctime std::cout<<asctime(timeInfo)<<std::endl; return 0; }