Skip to content

Commit 1131e84

Browse files
author
Anton Eriksson
committed
fix(python): Fix elapsed running time
1 parent 174c99f commit 1131e84

File tree

1 file changed

+2
-89
lines changed

1 file changed

+2
-89
lines changed

src/utils/Stopwatch.h

+2-89
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
#ifndef STOPWATCH_H_
1111
#define STOPWATCH_H_
1212

13-
#ifndef PYTHON
1413
#include <chrono>
1514
#include <ratio>
15+
#include <ostream>
16+
#include <cmath>
1617

1718
namespace infomap {
1819

@@ -99,92 +100,4 @@ class Stopwatch {
99100

100101
} // namespace infomap
101102

102-
#else
103-
104-
#include <ctime>
105-
106-
namespace infomap {
107-
108-
class Stopwatch {
109-
public:
110-
explicit Stopwatch(bool startImmediately)
111-
: m_start(0), m_stop(0), m_running(false)
112-
{
113-
if (startImmediately) {
114-
start();
115-
}
116-
}
117-
118-
void start()
119-
{
120-
m_start = std::clock();
121-
m_running = true;
122-
}
123-
124-
void reset()
125-
{
126-
if (m_running)
127-
m_start = std::clock();
128-
}
129-
130-
void stop()
131-
{
132-
if (m_running) {
133-
m_stop = std::clock();
134-
m_running = false;
135-
}
136-
}
137-
138-
double getElapsedTimeInSec() const
139-
{
140-
clock_t ticks = (m_running ? std::clock() : m_stop) - m_start;
141-
return (double)ticks / CLOCKS_PER_SEC;
142-
}
143-
144-
double getElapsedTimeInMilliSec() const
145-
{
146-
clock_t ticks = (m_running ? std::clock() : m_stop) - m_start;
147-
return ticks * 1000.0 / CLOCKS_PER_SEC;
148-
}
149-
150-
static double getElapsedTimeSinceProgramStartInSec()
151-
{
152-
return (double)std::clock() / CLOCKS_PER_SEC;
153-
}
154-
155-
static double getElapsedTimeSinceProgramStartInMilliSec()
156-
{
157-
return std::clock() * 1000.0 / CLOCKS_PER_SEC;
158-
}
159-
160-
friend std::ostream& operator<<(std::ostream& out, const Stopwatch& stopwatch)
161-
{
162-
unsigned int temp = static_cast<unsigned int>(std::floor(stopwatch.getElapsedTimeInMilliSec()));
163-
if (temp > 60'000) {
164-
if (temp > 3600'000) {
165-
if (temp > 86'400'000) {
166-
out << temp / 86'400'000 << "d ";
167-
temp %= 86'400'000;
168-
}
169-
out << temp / 3600'000 << "h ";
170-
temp %= 3600'000;
171-
}
172-
out << temp / 60'000 << "m ";
173-
temp %= 60'000;
174-
out << temp * 1.0 / 1000 << "s";
175-
} else {
176-
out << stopwatch.getElapsedTimeInSec() << "s";
177-
}
178-
return out;
179-
}
180-
181-
private:
182-
std::clock_t m_start, m_stop;
183-
bool m_running;
184-
};
185-
186-
} // namespace infomap
187-
188-
#endif
189-
190103
#endif // STOPWATCH_H_

0 commit comments

Comments
 (0)