Debian Codegraph @

src/utility/Stopwatch.cpp

raw
1 #include <pbcopper/utility/Stopwatch.h>
2
3 #include <sstream>
4 #include <type_traits>
5
6 namespace PacBio {
7 namespace Utility {
8
9 std::string Stopwatch::PrettyPrintNanoseconds(int64_t nanosecs)
10 {
11 auto d = nanosecs / 1000 / 1000 / 1000 / 60 / 60 / 24;
12 auto h = (nanosecs / 1000 / 1000 / 1000 / 60 / 60) % 24;
13 auto m = (nanosecs / 1000 / 1000 / 1000 / 60) % 60;
14 auto s = (nanosecs / 1000 / 1000 / 1000) % 60;
15 auto ms = (nanosecs / 1000 / 1000) % 1000;
16 auto us = (nanosecs / 1000) % 1000;
17 auto ns = nanosecs % 1000;
18 std::ostringstream ss;
19 if (d > 0) {
20 ss << std::to_string(d) << "d ";
21 }
22 if (h > 0) {
23 ss << std::to_string(h) << "h ";
24 }
25 if (m > 0 && d == 0) {
26 ss << std::to_string(m) << "m ";
27 }
28 if (s > 0 && h == 0) {
29 ss << std::to_string(s) << "s ";
30 }
31 if (ms > 0 && m == 0) {
32 ss << std::to_string(ms) << "ms ";
33 }
34 if (us > 0 && s == 0) {
35 ss << std::to_string(us) << "us ";
36 }
37 if (ns > 0 && ms == 0) {
38 ss << std::to_string(ns) << "ns ";
39 }
40 return ss.str();
41 }
42
43 double Stopwatch::CpuTime()
44 {
45 struct rusage r;
46 getrusage(RUSAGE_SELF, &r);
47 return r.ru_utime.tv_sec + r.ru_stime.tv_sec + 1e-6 * (r.ru_utime.tv_usec + r.ru_stime.tv_usec);
48 }
49 } // namespace Utility
50 } // namespace PacBio