diff options
author | vvvv <[email protected]> | 2023-07-31 18:21:04 +0300 |
---|---|---|
committer | vvvv <[email protected]> | 2023-07-31 18:21:04 +0300 |
commit | dec41c40e51aa407edef81a3c566a5a15780fc49 (patch) | |
tree | 4f197b596b32f35eca368121f0dff913419da9af /library/cpp/ipreg/stopwatch.cpp | |
parent | 3ca8b54c96e09eb2b65be7f09675623438d559c7 (diff) |
YQL-16239 Move purecalc to public
Diffstat (limited to 'library/cpp/ipreg/stopwatch.cpp')
-rw-r--r-- | library/cpp/ipreg/stopwatch.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/library/cpp/ipreg/stopwatch.cpp b/library/cpp/ipreg/stopwatch.cpp new file mode 100644 index 00000000000..31d99d2758d --- /dev/null +++ b/library/cpp/ipreg/stopwatch.cpp @@ -0,0 +1,53 @@ +#include "stopwatch.h" + +#include <util/stream/str.h> + +namespace NIPREG { + +TStopWatch::TStopWatch() { + Start = TInstant::Now(); +} + +TStopWatch::~TStopWatch() { + try { + if (TaskRunning) + StopTask(); + + Cerr << "Everything done in " << FormatTime(TInstant::Now() - Start) << Endl; + } catch (...) { + // not much problem if we can't write the summary + } +} + +void TStopWatch::StartTask(const TString& message) { + StopTask(); + + ++TaskOrdNum; + TaskStart = TInstant::Now(); + TaskRunning = true; + Cerr << TaskOrdNum << ". " << message << "...\n"; +} + +void TStopWatch::StopTask() { + if (TaskRunning) { + Cerr << "Done in " << FormatTime(TInstant::Now() - TaskStart) << Endl; + TaskRunning = false; + } +} + +TString TStopWatch::FormatTime(const TDuration& dur) { + auto sec = dur.Seconds(); + + TStringStream ss; + + if (sec < 60) + ss << sec << "s"; + else if (sec < 3600) + ss << sec / 60 << "m " << sec % 60 << "s"; + else + ss << sec / 3600 << "h " << (sec / 60) % 60 << "m"; + + return ss.Str(); +} + +} |