| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (C) 2010 by Thomas Moulard, CNRS. | ||
| 2 | // | ||
| 3 | |||
| 4 | // Redistribution and use in source and binary forms, with or without | ||
| 5 | // modification, are permitted provided that the following conditions are | ||
| 6 | // met: | ||
| 7 | // | ||
| 8 | // 1. Redistributions of source code must retain the above copyright | ||
| 9 | // notice, this list of conditions and the following disclaimer. | ||
| 10 | // | ||
| 11 | // 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | // notice, this list of conditions and the following disclaimer in the | ||
| 13 | // documentation and/or other materials provided with the distribution. | ||
| 14 | // | ||
| 15 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 16 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 17 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| 18 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| 19 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 20 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| 21 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| 22 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| 23 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 24 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| 25 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | ||
| 26 | // DAMAGE. | ||
| 27 | |||
| 28 | #include "hpp/util/timer.hh" | ||
| 29 | |||
| 30 | #include <iomanip> | ||
| 31 | #include <iostream> | ||
| 32 | |||
| 33 | using namespace std::chrono; | ||
| 34 | |||
| 35 | namespace hpp { | ||
| 36 | namespace debug { | ||
| 37 | ✗ | Timer::Timer(bool autoStart) : start_(), end_() { | |
| 38 | ✗ | if (autoStart) start(); | |
| 39 | ✗ | } | |
| 40 | |||
| 41 | ✗ | Timer::Timer(const Timer& timer) : start_(timer.start_), end_(timer.end_) {} | |
| 42 | |||
| 43 | ✗ | Timer& Timer::operator=(const Timer& timer) { | |
| 44 | ✗ | if (this == &timer) return *this; | |
| 45 | ✗ | start_ = timer.start_; | |
| 46 | ✗ | end_ = timer.end_; | |
| 47 | ✗ | return *this; | |
| 48 | } | ||
| 49 | |||
| 50 | ✗ | Timer::~Timer() {} | |
| 51 | |||
| 52 | ✗ | const Timer::time_point& Timer::start() { return start_ = clock_type::now(); } | |
| 53 | |||
| 54 | ✗ | const Timer::time_point& Timer::stop() { return end_ = clock_type::now(); } | |
| 55 | |||
| 56 | ✗ | const Timer::time_point& Timer::getStart() const { return start_; } | |
| 57 | |||
| 58 | ✗ | const Timer::time_point& Timer::getStop() const { return end_; } | |
| 59 | |||
| 60 | ✗ | double Timer::duration() const { return duration_type(end_ - start_).count(); } | |
| 61 | |||
| 62 | ✗ | std::ostream& Timer::print(std::ostream& o) const { | |
| 63 | ✗ | auto time = system_clock::to_time_t( | |
| 64 | ✗ | system_clock::now() + | |
| 65 | ✗ | duration_cast<system_clock::duration>(start_ - clock_type::now())); | |
| 66 | |||
| 67 | return o << "timer started at " | ||
| 68 | ✗ | << std::put_time(std::localtime(&time), "%F %T") | |
| 69 | << " and elapsed time " | ||
| 70 | ✗ | "is " | |
| 71 | ✗ | << duration(); | |
| 72 | } | ||
| 73 | |||
| 74 | 2 | TimeCounter::TimeCounter(const std::string& name) | |
| 75 | 2 | : n_(name), | |
| 76 | 2 | c_(0), | |
| 77 | 2 | t_(duration_type::zero()), | |
| 78 | 2 | min_(duration_type::max()), | |
| 79 | 2 | max_(duration_type::min()) {} | |
| 80 | |||
| 81 | 20 | void TimeCounter::start() { s_ = clock_type::now(); } | |
| 82 | |||
| 83 | 20 | double TimeCounter::stop() { | |
| 84 |
2/4✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
|
20 | last_ = clock_type::now() - s_; |
| 85 | 20 | min_ = std::min(last_, min_); | |
| 86 | 20 | max_ = std::max(last_, max_); | |
| 87 | 20 | t_ += last_; | |
| 88 | 20 | ++c_; | |
| 89 | 20 | return last_.count(); | |
| 90 | } | ||
| 91 | |||
| 92 | 20 | double TimeCounter::last() { return last_.count(); } | |
| 93 | |||
| 94 | ✗ | void TimeCounter::reset() { | |
| 95 | ✗ | t_ = duration_type::zero(); | |
| 96 | ✗ | c_ = 0; | |
| 97 | ✗ | min_ = duration_type::max(); | |
| 98 | ✗ | max_ = duration_type::min(); | |
| 99 | ✗ | } | |
| 100 | |||
| 101 | 2 | double TimeCounter::min() const { return min_.count(); } | |
| 102 | |||
| 103 | 2 | double TimeCounter::max() const { return max_.count(); } | |
| 104 | |||
| 105 | 2 | double TimeCounter::mean() const { | |
| 106 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
2 | return (c_ > 0) ? (t_ / (int)c_).count() : 0; |
| 107 | } | ||
| 108 | |||
| 109 | 2 | double TimeCounter::totalTime() const { return t_.count(); } | |
| 110 | |||
| 111 | 2 | std::ostream& TimeCounter::print(std::ostream& os) const { | |
| 112 | 2 | return os << "Time Counter " << n_ << ": " << c_ << ", " << totalTime() | |
| 113 | 2 | << ", [ " << min() << ", " << mean() << ", " << max() << "]"; | |
| 114 | } | ||
| 115 | |||
| 116 | 2 | std::ostream& operator<<(std::ostream& os, const TimeCounter& tc) { | |
| 117 | 2 | return tc.print(os); | |
| 118 | } | ||
| 119 | } // end of namespace debug | ||
| 120 | } // end of namespace hpp | ||
| 121 |