mirror of
https://github.com/Z3Prover/z3
synced 2025-04-15 13:28:47 +00:00
modernize stopwatch
This commit is contained in:
parent
a76c0fbbfb
commit
3a5263be95
|
@ -20,171 +20,55 @@ Revision History:
|
||||||
#ifndef STOPWATCH_H_
|
#ifndef STOPWATCH_H_
|
||||||
#define STOPWATCH_H_
|
#define STOPWATCH_H_
|
||||||
|
|
||||||
#if defined(_WINDOWS) || defined(_CYGWIN) || defined(_MINGW)
|
#include "util/debug.h"
|
||||||
|
#include <chrono>
|
||||||
// Does this redefinition work?
|
|
||||||
|
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
class stopwatch
|
class stopwatch
|
||||||
{
|
{
|
||||||
private:
|
std::chrono::time_point<std::chrono::steady_clock> m_start;
|
||||||
LARGE_INTEGER m_elapsed;
|
std::chrono::steady_clock::duration m_elapsed;
|
||||||
LARGE_INTEGER m_last_start_time;
|
#if Z3DEBUG
|
||||||
LARGE_INTEGER m_last_stop_time;
|
bool m_running = false;
|
||||||
LARGE_INTEGER m_frequency;
|
#endif
|
||||||
|
|
||||||
|
static std::chrono::time_point<std::chrono::steady_clock> get() {
|
||||||
|
return std::chrono::steady_clock::now();
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
stopwatch() {
|
stopwatch() {
|
||||||
QueryPerformanceFrequency(&m_frequency);
|
reset();
|
||||||
reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~stopwatch() {};
|
void add(const stopwatch &s) {
|
||||||
|
m_elapsed += s.m_elapsed;
|
||||||
void add (const stopwatch &s) {/* TODO */}
|
|
||||||
|
|
||||||
void reset() { m_elapsed.QuadPart = 0; }
|
|
||||||
|
|
||||||
void start() {
|
|
||||||
QueryPerformanceCounter(&m_last_start_time);
|
|
||||||
}
|
|
||||||
|
|
||||||
void stop() {
|
|
||||||
QueryPerformanceCounter(&m_last_stop_time);
|
|
||||||
m_elapsed.QuadPart += m_last_stop_time.QuadPart - m_last_start_time.QuadPart;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double get_seconds() const {
|
void reset() {
|
||||||
return static_cast<double>(m_elapsed.QuadPart / static_cast<double>(m_frequency.QuadPart)) ;
|
m_elapsed = std::chrono::steady_clock::duration::zero();
|
||||||
|
}
|
||||||
|
|
||||||
|
void start() {
|
||||||
|
SASSERT(!m_running);
|
||||||
|
DEBUG_CODE(m_running = true);
|
||||||
|
m_start = get();
|
||||||
|
}
|
||||||
|
|
||||||
|
void stop() {
|
||||||
|
SASSERT(m_running);
|
||||||
|
DEBUG_CODE(m_running = false);
|
||||||
|
m_elapsed += get() - m_start;
|
||||||
|
}
|
||||||
|
|
||||||
|
double get_seconds() const {
|
||||||
|
return std::chrono::duration_cast<std::chrono::milliseconds>(m_elapsed).count() / 1000.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
double get_current_seconds() const {
|
double get_current_seconds() const {
|
||||||
LARGE_INTEGER t;
|
return std::chrono::duration_cast<std::chrono::milliseconds>(get() - m_start).count() / 1000.0;
|
||||||
QueryPerformanceCounter(&t);
|
|
||||||
return static_cast<double>( (t.QuadPart - m_last_start_time.QuadPart) / static_cast<double>(m_frequency.QuadPart));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#undef max
|
|
||||||
#undef min
|
|
||||||
|
|
||||||
|
|
||||||
#elif defined(__APPLE__) && defined (__MACH__) // macOS
|
|
||||||
|
|
||||||
#include<mach/mach.h>
|
|
||||||
#include<mach/clock.h>
|
|
||||||
|
|
||||||
class stopwatch {
|
|
||||||
unsigned long long m_time; // elapsed time in ns
|
|
||||||
bool m_running;
|
|
||||||
clock_serv_t m_host_clock;
|
|
||||||
mach_timespec_t m_start;
|
|
||||||
|
|
||||||
public:
|
|
||||||
stopwatch():m_time(0), m_running(false) {
|
|
||||||
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &m_host_clock);
|
|
||||||
}
|
|
||||||
|
|
||||||
~stopwatch() {}
|
|
||||||
|
|
||||||
void add (const stopwatch &s) {m_time += s.m_time;}
|
|
||||||
|
|
||||||
void reset() {
|
|
||||||
m_time = 0ull;
|
|
||||||
}
|
|
||||||
|
|
||||||
void start() {
|
|
||||||
if (!m_running) {
|
|
||||||
clock_get_time(m_host_clock, &m_start);
|
|
||||||
m_running = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void stop() {
|
|
||||||
if (m_running) {
|
|
||||||
mach_timespec_t _stop;
|
|
||||||
clock_get_time(m_host_clock, &_stop);
|
|
||||||
m_time += (_stop.tv_sec - m_start.tv_sec) * 1000000000ull;
|
|
||||||
m_time += (_stop.tv_nsec - m_start.tv_nsec);
|
|
||||||
m_running = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
double get_seconds() const {
|
|
||||||
if (m_running) {
|
|
||||||
const_cast<stopwatch*>(this)->stop();
|
|
||||||
/* update m_time */
|
|
||||||
const_cast<stopwatch*>(this)->start();
|
|
||||||
}
|
|
||||||
return static_cast<double>(m_time)/static_cast<double>(1000000000ull);
|
|
||||||
}
|
|
||||||
|
|
||||||
double get_current_seconds() const {
|
|
||||||
return get_seconds();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#else // Linux
|
|
||||||
|
|
||||||
#include<ctime>
|
|
||||||
|
|
||||||
#ifndef CLOCK_PROCESS_CPUTIME_ID
|
|
||||||
/* BSD */
|
|
||||||
# define CLOCK_PROCESS_CPUTIME_ID CLOCK_MONOTONIC
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class stopwatch {
|
|
||||||
unsigned long long m_time; // elapsed time in ns
|
|
||||||
bool m_running;
|
|
||||||
struct timespec m_start;
|
|
||||||
|
|
||||||
public:
|
|
||||||
stopwatch():m_time(0), m_running(false) {
|
|
||||||
}
|
|
||||||
|
|
||||||
~stopwatch() {}
|
|
||||||
|
|
||||||
void add (const stopwatch &s) {m_time += s.m_time;}
|
|
||||||
|
|
||||||
void reset() {
|
|
||||||
m_time = 0ull;
|
|
||||||
}
|
|
||||||
|
|
||||||
void start() {
|
|
||||||
if (!m_running) {
|
|
||||||
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &m_start);
|
|
||||||
m_running = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void stop() {
|
|
||||||
if (m_running) {
|
|
||||||
struct timespec _stop;
|
|
||||||
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &_stop);
|
|
||||||
m_time += (_stop.tv_sec - m_start.tv_sec) * 1000000000ull;
|
|
||||||
if (m_time != 0 || _stop.tv_nsec >= m_start.tv_nsec)
|
|
||||||
m_time += (_stop.tv_nsec - m_start.tv_nsec);
|
|
||||||
m_running = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
double get_seconds() const {
|
|
||||||
if (m_running) {
|
|
||||||
const_cast<stopwatch*>(this)->stop();
|
|
||||||
/* update m_time */
|
|
||||||
const_cast<stopwatch*>(this)->start();
|
|
||||||
}
|
|
||||||
return static_cast<double>(m_time)/static_cast<double>(1000000000ull);
|
|
||||||
}
|
|
||||||
|
|
||||||
double get_current_seconds() const {
|
|
||||||
return get_seconds();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct scoped_watch {
|
struct scoped_watch {
|
||||||
stopwatch &m_sw;
|
stopwatch &m_sw;
|
||||||
|
|
Loading…
Reference in a new issue