方法一:用clock_t
#include <time.h> #include <iostream> using namespace std; clock_t begin = clock(); .............. clock_t end = clock(); double cost = (double)(end - begin)/ (double)CLOCKS_PER_SEC * 1000; cout << "运行时间:" << cost << "ms" << endl;
方法二: 用time_t
#include <time.h> time_t begin; time_t end; begin = time(NULL); ................程序代码 end = time(NULL); cout << "程序运行时间: " << end - begin << "second " << endl;