#include using namespace std; void f() { volatile double d = 0; for (int n = 0; n != 10000; ++n) for (int m = 0; m != 10000; ++m) d += d * n * m; } void test_clock() { const std::clock_t c_start = std::clock(); auto t_start = std::chrono::high_resolution_clock::now(); std::thread t1(f); std::thread t2(f); // f() is called on two threads t1.join(); t2.join(); const std::clock_t c_end = std::clock(); const auto t_end = std::chrono::high_resolution_clock::now(); cout << std::fixed << std::setprecision(2) << "CPU time used: " << 1000.0 * (c_end - c_start) / CLOCKS_PER_SEC << "ms\n" << endl; auto duration = t_end - t_start; cout << "Wall clock time passed: " << duration << endl; } int main(void) { struct timespec ts1, ts2, diff; clock_gettime(CLOCK_MONOTONIC_RAW, &ts1); cout << ts1.tv_sec << " " << ts1.tv_nsec << endl; std::this_thread::sleep_for(std::chrono::microseconds(3)); clock_gettime(CLOCK_MONOTONIC_RAW, &ts2); cout << ts2.tv_sec << " " << ts2.tv_nsec << endl; diff.tv_sec = ts2.tv_sec - ts1.tv_sec; diff.tv_nsec = ts2.tv_nsec - ts1.tv_nsec; cout << " sleep_for: 3us" << endl; cout << " diff.tv_sec: " << diff.tv_sec << endl; cout << "diff.tv_nsec: " << diff.tv_nsec << endl; cout << "time diff(us): " << diff.tv_sec + diff.tv_nsec/1000 << endl; test_clock(); return 0; }