timer.h 665 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #pragma once
  2. #ifndef TIMER_H
  3. #define TIMER_H
  4. #include <ctime>
  5. #include <chrono>
  6. #include <stdio.h>
  7. class Timer
  8. {
  9. public:
  10. Timer(const char *nameIn)
  11. {
  12. name = nameIn;
  13. tic();
  14. }
  15. void tic()
  16. {
  17. start = std::chrono::system_clock::now();
  18. }
  19. double toc()
  20. {
  21. end = std::chrono::system_clock::now();
  22. std::chrono::duration<double> dt = end - start;
  23. return dt.count() * 1000;
  24. }
  25. void tic_toc()
  26. {
  27. printf("The process %s takes %f ms.\n", name, toc());
  28. }
  29. private:
  30. const char* name;
  31. std::chrono::time_point<std::chrono::system_clock> start, end;
  32. };
  33. #endif // TIMER_H