需要c++11支持
#include <iostream> #include <chrono> using std::chrono::high_resolution_clock; using std::chrono::milliseconds; using std::chrono::duration_cast; int main() { high_resolution_clock::time_point start, t; int num = 10000000; // 开始时间 start = high_resolution_clock::now(); // 1000W次测试 for (int i=0;i<num;i++) { t = high_resolution_clock::now(); } // 保存时间间隔 milliseconds timeInterval = duration_cast<milliseconds>(t - start); std::cout << timeInterval.count() << std::endl; return 0; }
获取时间的同时,对该方法进行1000W次测试,只需350毫秒