相关代码:
#include <iostream>
#include <chrono>
#include <unistd.h>
int main(){
auto start = std::chrono::steady_clock::now();
// sleep(1);
for(int i = 0; i < 100000; i++){
int a = 3 + 2;
}
auto end = std::chrono::steady_clock::now();
std::cout << "cost ms: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms." << std::endl;
std::cout << "cost us: " << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << " us." << std::endl;
std::cout << "cost ns: " << std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count() << " ns." << std::endl;
return 0;
}
执行结果:
cost ms: 0 ms.
cost us: 186 us.
cost ns: 186405 ns.
cpu是i7 11700.