1、采用clock()来实现
clock_t start = clock();
// do something...
clock_t end = clock();
cout << "time is " << (double)(end - start) / CLOCKS_PER_SEC << "s" << endl;
2、采用c++11 的chrono
std::milli:毫秒
std::micro:微秒
std::nano:纳秒
auto start = std::chrono::system_clock::now();
//do something
auto end = std::chrono::system_clock::now();
double t = std::chrono::duration<double, std::milli>(end - start).count();//毫秒
//也可这么转
double t = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();//微秒
auto tt = duration_cast<microseconds>(end - start);//用duration_cast来转换也可以
chrono休眠
std::this_thread::sleep_for(std::chrono::milliseconds(1));
// or
std::this_thread::sleep_for(std::chrono::duration<long long, std::milli>(1));