Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _bench_h_
- #define _bench_h_
- #include <iostream>
- #include <iomanip>
- #include <boost/format.hpp>
- #include <boost/foreach.hpp>
- #include <time.h>
- #ifndef NSEC_PER_SEC
- # define NSEC_PER_SEC 1000000000ULL
- #endif
- inline static long timespec_diff_ns(struct timespec *start, struct timespec *end)
- {
- long ret;
- ret = (long)(end->tv_sec - start->tv_sec)*NSEC_PER_SEC;
- ret += (long)(end->tv_nsec - start->tv_nsec);
- return ret;
- }
- class Bench
- {
- public:
- Bench(const std::string& benchname)
- : m_benchname(benchname)
- {
- start();
- }
- void start()
- {
- ::clock_gettime(CLOCK_MONOTONIC, &m_ts_start);
- }
- void end()
- {
- ::clock_gettime(CLOCK_MONOTONIC, &m_ts_end);
- }
- void cp(const char* cpname)
- {
- struct checkpoint cp;
- cp.name = cpname;
- ::clock_gettime(CLOCK_MONOTONIC, &cp.ts);
- m_cps.push_back(cp);
- }
- void dump()
- {
- std::cout << "hgrev\tbenchname\ttotal";
- BOOST_FOREACH(struct checkpoint& cp, m_cps)
- {
- std::cout << '\t' << cp.name;
- }
- std::cout << std::endl;
- 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;
- BOOST_FOREACH(struct checkpoint& cp, m_cps)
- {
- std::cout << '\t' << std::setiosflags(std::ios::fixed) << std::setprecision(4) << (double)timespec_diff_ns(&m_ts_start, &cp.ts) / NSEC_PER_SEC;
- }
- std::cout << std::endl;
- }
- private:
- std::string m_benchname;
- struct timespec m_ts_start, m_ts_end;
- struct checkpoint
- {
- struct timespec ts;
- const char* name;
- };
- std::vector<checkpoint> m_cps;
- };
- #endif // _bench_h_
Add Comment
Please, Sign In to add comment