timer.H 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // (c) Daniel Llorens - 2005
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. #ifndef RA_TIMER_H
  7. #define RA_TIMER_H
  8. /// @file timer.H
  9. /// @brief Utility for measuring time.
  10. #include <ctime>
  11. #include <sys/time.h>
  12. #include <sys/resource.h>
  13. #include <string>
  14. #include <iosfwd>
  15. #include <sstream>
  16. #include <limits>
  17. class Timer
  18. {
  19. // run time.
  20. clock_t last_run;
  21. double gone_run;
  22. // run time, otherwise.
  23. timeval last_usr;
  24. timeval last_sys;
  25. double gone_usr;
  26. double gone_sys;
  27. // wall time.
  28. timeval last_wall;
  29. double gone_wall;
  30. public:
  31. // fills a string with the current time and date.
  32. static std::string const timeAndDate()
  33. {
  34. std::time_t t;
  35. time(&t);
  36. std::string tad(std::ctime(&t));
  37. return tad;
  38. }
  39. // convert number of seconds into formatted string.
  40. static std::string stime(double const & d)
  41. {
  42. double dd = d;
  43. std::ostringstream os;
  44. if (d<1.e-3) {
  45. os << int(dd*1.e6) << "us ";
  46. } else if (d<1.) {
  47. os << int(dd*1.e3) << "ms ";
  48. } else {
  49. if (dd>86400.) {
  50. os << int(dd/86400) << "d ";
  51. dd = dd-int(dd/86400.)*86400.;
  52. }
  53. if (dd>3600.) {
  54. os << int(dd/3600.) << "h ";
  55. dd = dd-int(dd/3600.)*3600.;
  56. }
  57. if (d>60.) {
  58. os << int(dd/60.) << "m ";
  59. dd = dd-int(dd/60.)*60.;
  60. }
  61. os << dd << "s";
  62. }
  63. return os.str();
  64. }
  65. // constructor does not need any parameters. The count must still be started.
  66. Timer(bool const do_start = false)
  67. {
  68. if (do_start) {
  69. start();
  70. }
  71. }
  72. // reset and start the count.
  73. void start()
  74. {
  75. last_run = clock();
  76. gettimeofday(&last_wall, 0);
  77. {
  78. rusage tmp;
  79. getrusage(RUSAGE_SELF, &tmp);
  80. last_sys = tmp.ru_stime;
  81. last_usr = tmp.ru_utime;
  82. }
  83. }
  84. // record the count.
  85. void stop()
  86. {
  87. gone_run = double(clock()-last_run)/CLOCKS_PER_SEC;
  88. {
  89. timeval t;
  90. gettimeofday(&t, 0);
  91. gone_wall = double(t.tv_sec-last_wall.tv_sec); // @CONVERSION, etc
  92. gone_wall += double(t.tv_usec-last_wall.tv_usec)*1e-6;
  93. }
  94. {
  95. rusage t;
  96. getrusage(RUSAGE_SELF, &t);
  97. gone_sys = double(t.ru_stime.tv_sec-last_sys.tv_sec); // @CONVERSION, etc.
  98. gone_sys += double(t.ru_stime.tv_usec-last_sys.tv_usec)*1e-6;
  99. gone_usr = double(t.ru_utime.tv_sec-last_usr.tv_sec);
  100. gone_usr += double(t.ru_utime.tv_usec-last_usr.tv_usec)*1e-6;
  101. }
  102. }
  103. double run() const { return gone_run; }
  104. double wall() const { return gone_wall; }
  105. double cpu() const { return gone_sys+gone_usr; }
  106. double sys() const { return gone_sys; }
  107. double usr() const { return gone_usr; }
  108. std::string get_string() const
  109. {
  110. return stime(wall());
  111. }
  112. };
  113. inline std::ostream&
  114. operator<<(std::ostream& o, Timer& timer)
  115. {
  116. timer.stop();
  117. return o << timer.get_string();
  118. }
  119. #endif // RA_TIMER_H