Guest User

Untitled

a guest
Jul 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #ifndef _bench_h_
  2. #define _bench_h_
  3.  
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <boost/format.hpp>
  7. #include <boost/foreach.hpp>
  8. #include <time.h>
  9.  
  10. #ifndef NSEC_PER_SEC
  11. # define NSEC_PER_SEC 1000000000ULL
  12. #endif
  13.  
  14. inline static long timespec_diff_ns(struct timespec *start, struct timespec *end)
  15. {
  16. long ret;
  17.  
  18. ret = (long)(end->tv_sec - start->tv_sec)*NSEC_PER_SEC;
  19. ret += (long)(end->tv_nsec - start->tv_nsec);
  20. return ret;
  21. }
  22.  
  23. class Bench
  24. {
  25. public:
  26. Bench(const std::string& benchname)
  27. : m_benchname(benchname)
  28. {
  29. start();
  30. }
  31.  
  32. void start()
  33. {
  34. ::clock_gettime(CLOCK_MONOTONIC, &m_ts_start);
  35. }
  36.  
  37. void end()
  38. {
  39. ::clock_gettime(CLOCK_MONOTONIC, &m_ts_end);
  40. }
  41.  
  42. void cp(const char* cpname)
  43. {
  44. struct checkpoint cp;
  45. cp.name = cpname;
  46. ::clock_gettime(CLOCK_MONOTONIC, &cp.ts);
  47. m_cps.push_back(cp);
  48. }
  49.  
  50. void dump()
  51. {
  52. std::cout << "hgrev\tbenchname\ttotal";
  53. BOOST_FOREACH(struct checkpoint& cp, m_cps)
  54. {
  55. std::cout << '\t' << cp.name;
  56. }
  57. std::cout << std::endl;
  58. std::cout << HGNUM << '\t' << m_benchname << '\t' << std::setiosflags(std::ios::fixed) << std::setprecision(4) << (double)timespec_diff_ns(&m_ts_start, &m_ts_end) / NSEC_PER_SEC;
  59. BOOST_FOREACH(struct checkpoint& cp, m_cps)
  60. {
  61. std::cout << '\t' << std::setiosflags(std::ios::fixed) << std::setprecision(4) << (double)timespec_diff_ns(&m_ts_start, &cp.ts) / NSEC_PER_SEC;
  62. }
  63. std::cout << std::endl;
  64. }
  65.  
  66. private:
  67. std::string m_benchname;
  68.  
  69. struct timespec m_ts_start, m_ts_end;
  70. struct checkpoint
  71. {
  72. struct timespec ts;
  73. const char* name;
  74. };
  75. std::vector<checkpoint> m_cps;
  76. };
  77.  
  78. #endif // _bench_h_
Add Comment
Please, Sign In to add comment