fork download
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <thread>
  4. #include <iostream>
  5. #include <chrono>
  6.  
  7. int main(void) {
  8.  
  9. std::cout << "sleep(3) took: \n\n";
  10.  
  11. clock_t c_start, c_end;
  12. time_t t_start, t_end;
  13. std::chrono::high_resolution_clock::time_point h_start, h_end;
  14. std::chrono::steady_clock::time_point steady_start, steady_end;
  15.  
  16. time(&t_start); // less precise than clock() but always get the real actual time
  17. c_start = clock(); // clock() get only CPU-time, it can be more than real or less - sleep(3); took 0.00 seconds
  18. h_start = std::chrono::high_resolution_clock::now();
  19. steady_start = std::chrono::steady_clock::now();
  20.  
  21. std::this_thread::sleep_for(std::chrono::seconds(3));
  22.  
  23. steady_end = std::chrono::steady_clock::now();
  24. h_end = std::chrono::high_resolution_clock::now();
  25. c_end = clock();
  26. time(&t_end);
  27.  
  28. std::cout << "highres = " << std::chrono::duration<double>(h_end - h_start).count() << " s \n";
  29. std::cout << "steady = " << std::chrono::duration<double>(steady_end - steady_start).count() << " s \n";
  30.  
  31. printf("clock() = %.2lf seconds \n", (c_end - c_start) / (double)CLOCKS_PER_SEC);
  32. printf("time() = %.2lf seconds \n", difftime(t_end, t_start));
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
sleep(3) took: 

highres = 3.00098 s 
steady = 3.00098 s 
clock() = 0.00 seconds 
time() = 3.00 seconds