не работает duration_cast в библиотеке chrono
Я написал класс таймера.
timer.cpp:
#include "timer.h"
#include <cassert>
using namespace std::chrono;
Timer::Timer()
{
}
void Timer::start()
{
t1 = steady_clock::now();
started_now = true;
started = true;
}
void Timer::stop()
{
t2 = steady_clock::now();
started_now = false;
}
void Timer::check()
{
assert(started && "Timer didnt start");
}
duration<double> Timer::getTime()
{
#ifndef NDEBUG
check();
#endif
if (started_now)
t2 = steady_clock::now();
duration<double> elapsed_time = t2 - t1;
return elapsed_time;
}
double Timer::getSeconds()
{
auto time = getTime();
duration<double> res = duration_cast<duration<double, std::ratio<1>>>(time);
return res.count();
}
double Timer::getMilliseconds()
{
auto time = getTime();
duration<double> res = duration_cast<duration<double, std::milli>>(time);
return res.count();
}
timer.h:
#pragma once
#include <chrono>
class Timer
{
std::chrono::steady_clock::time_point t1;
std::chrono::steady_clock::time_point t2;
void check();
std::chrono::duration<double> getTime();
public:
bool started_now;
bool started;
Timer();
void start();
void stop();
double getSeconds();
double getMilliseconds();
};
getSeconds возвращает время в секундах, а getMilliseconds - в миллисекундах.
Почему эти функции возвращают одно и то же значение?
Timer timer = Timer();
timer.start();
/* ... */
timer.stop();
cout << "Seconds: " << timer.getSeconds() << endl;
cout << "Milliseconds: " << timer.getMilliseconds() << endl;
Вывод:
Seconds: 0.129542
Milliseconds: 0.129542