-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #469 from dirac-institute/debug_timer
Add DebugTimer to the pybind bindings
- Loading branch information
Showing
6 changed files
with
156 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* A very simple class that wraps debug output with timing. Used for | ||
providing status updates as the system is running if in verbose | ||
mode and nothing otherwise. | ||
This is *not* a high precision timer meant to be used for benchmarking. | ||
*/ | ||
|
||
#include "debug_timer.h" | ||
#include "pydocs/debug_timer_docs.h" | ||
|
||
namespace search { | ||
|
||
DebugTimer::DebugTimer(std::string name, bool verbose) { | ||
name_ = name; | ||
verbose_ = verbose; | ||
start(); | ||
} | ||
|
||
void DebugTimer::start() { | ||
running_ = true; | ||
t_start_ = std::chrono::system_clock::now(); | ||
if (verbose_) { | ||
std::cout << "Starting " << name_ << "...\n" << std::flush; | ||
} | ||
} | ||
|
||
void DebugTimer::stop() { | ||
t_end_ = std::chrono::system_clock::now(); | ||
running_ = false; | ||
|
||
if (verbose_) { | ||
auto t_delta = std::chrono::duration_cast<std::chrono::milliseconds>(t_end_ - t_start_); | ||
std::cout << name_ << " finished in " << t_delta.count() / 1000.0 << " seconds.\n" << std::flush; | ||
} | ||
} | ||
|
||
double DebugTimer::read() { | ||
std::chrono::milliseconds t_delta; | ||
if (running_) { | ||
std::chrono::time_point<std::chrono::system_clock> t_current_ = std::chrono::system_clock::now(); | ||
t_delta = std::chrono::duration_cast<std::chrono::milliseconds>(t_current_ - t_start_); | ||
} else { | ||
t_delta = std::chrono::duration_cast<std::chrono::milliseconds>(t_end_ - t_start_); | ||
} | ||
|
||
double result = t_delta.count() / 1000.0; | ||
if (verbose_) { | ||
std::cout << name_ << " at " << result << " seconds.\n" << std::flush; | ||
} | ||
return result; | ||
} | ||
|
||
#ifdef Py_PYTHON_H | ||
static void debug_timer_binding(py::module& m) { | ||
using dbt = search::DebugTimer; | ||
py::class_<dbt>(m, "DebugTimer", pydocs::DOC_DEBUG_TIMER) | ||
.def(py::init<std::string, bool>()) | ||
.def("start", &dbt::start, pydocs::DOC_DEBUG_TIMER_start) | ||
.def("stop", &dbt::stop, pydocs::DOC_DEBUG_TIMER_stop) | ||
.def("read", &dbt::read, pydocs::DOC_DEBUG_TIMER_read); | ||
} | ||
#endif /* Py_PYTHON_H */ | ||
|
||
} /* namespace search */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef DEBUG_TIMER_DOCS_ | ||
#define DEBUG_TIMER_DOCS_ | ||
|
||
namespace pydocs { | ||
static const auto DOC_DEBUG_TIMER = R"doc( | ||
A simple timer used for consistent outputing of timing results in verbose mode. | ||
Timer automatically starts when it is created. | ||
Parameters | ||
---------- | ||
name : `str` | ||
The name string of the timer. Used for ouput. | ||
verbose : `bool` | ||
Output the timing information to the standard output. | ||
)doc"; | ||
|
||
static const auto DOC_DEBUG_TIMER_start = R"doc( | ||
Start (or restart) the timer. If verbose outputs a message. | ||
)doc"; | ||
|
||
static const auto DOC_DEBUG_TIMER_stop = R"doc( | ||
Stop the timer. If verbose outputs the duration. | ||
)doc"; | ||
|
||
static const auto DOC_DEBUG_TIMER_read = R"doc( | ||
Read the timer duration as decimal seconds. | ||
Returns | ||
------- | ||
duration : `float` | ||
The duration of the timer. | ||
)doc"; | ||
|
||
} // namespace pydocs | ||
|
||
#endif /* #define DEBUG_TIMER_DOCS_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import time | ||
import unittest | ||
|
||
from kbmod.search import DebugTimer | ||
|
||
|
||
class test_debug_timer(unittest.TestCase): | ||
def test_create(self): | ||
my_timer = DebugTimer("hi", False) | ||
time1 = my_timer.read() | ||
|
||
# We use sleep (100ms) because we are only interested in | ||
# wall clock time having increased. | ||
time.sleep(0.1) | ||
time2 = my_timer.read() | ||
self.assertGreater(time2, time1) | ||
|
||
my_timer.stop() | ||
time3 = my_timer.read() | ||
time.sleep(0.1) | ||
time4 = my_timer.read() | ||
self.assertAlmostEqual(time3, time4) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |