clock.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef _clock_hpp_zzj_
  2. #define _clock_hpp_zzj_
  3. #include <time.h>
  4. #ifdef _MSC_VER
  5. struct zclock
  6. {
  7. unsigned m_start;
  8. zclock()
  9. {
  10. m_start=GetTickCount ();
  11. }
  12. long long count_us() const
  13. {
  14. return 1000L*count_ms();
  15. }
  16. unsigned count_ms()const
  17. {
  18. return (GetTickCount()-m_start);
  19. }
  20. void reset()
  21. {
  22. m_start=GetTickCount ();
  23. }
  24. long long pin_us()
  25. {
  26. return 1000L*pin_ms();
  27. }
  28. unsigned pin_ms()
  29. {
  30. unsigned rc=count_ms();
  31. m_start=GetTickCount();
  32. return rc;
  33. }
  34. };
  35. #else
  36. struct zclock
  37. {
  38. struct timespec m_start;
  39. zclock()
  40. {
  41. clock_gettime(CLOCK_MONOTONIC,&m_start);
  42. }
  43. void reset()
  44. {
  45. clock_gettime(CLOCK_MONOTONIC,&m_start);
  46. }
  47. long long count_us() const
  48. {
  49. struct timespec m_tmp;
  50. clock_gettime(CLOCK_MONOTONIC,&m_tmp);
  51. long long ret=(m_tmp.tv_sec-m_start.tv_sec)*1000000;
  52. ret+=(m_tmp.tv_nsec-m_start.tv_nsec)/1000;
  53. return ret;
  54. }
  55. unsigned count_ms()const
  56. {
  57. return count_us()/1000;
  58. }
  59. long long pin_us()
  60. {
  61. struct timespec m_tmp;
  62. clock_gettime(CLOCK_MONOTONIC,&m_tmp);
  63. long long ret=(m_tmp.tv_sec-m_start.tv_sec)*1000000;
  64. ret+=(m_tmp.tv_nsec-m_start.tv_nsec)/1000;
  65. m_start=m_tmp;
  66. return ret;
  67. }
  68. unsigned pin_ms()
  69. {
  70. return pin_us()/1000;
  71. }
  72. };
  73. #endif
  74. #endif