
#ifndef STOPWATCH_HPP
#define STOPWATCH_HPP

#include <chrono>

template <typename Clock = std::chrono::steady_clock>
class stopwatch
{
public:
	typedef Clock clock;
	typedef typename clock::time_point time_point;
	typedef typename clock::duration duration;

private:
	time_point last_;

public:
	stopwatch()
		: last_(clock::now())
	{}

	void reset()
	{
		*this = stopwatch();
	}

	time_point now() const
	{
		return clock::now();
	}

	duration elapsed() const
	{
		return now() - last_;
	}

	duration tick()
	{
		time_point dummy;
		return tick(dummy);
	}

	duration tick(time_point& now_)
	{
		now_ = now();
		auto elapsed = now_ - last_;
		last_ = now_;
		return elapsed;
	}
};

typedef stopwatch<> default_stopwatch;

template <typename T, typename Rep, typename Period>
T duration_cast(const std::chrono::duration<Rep, Period>& duration)
{
	return duration.count() * static_cast<T>(Period::num) / static_cast<T>(Period::den);
}

#endif


// cpp file
#include <iostream>
#include <thread>

int main()
{
	default_stopwatch sw;
	for (unsigned i = 0; i != 50; ++i)
	{
		std::this_thread::sleep_for(std::chrono::milliseconds(77));
		std::cout << duration_cast<double>(sw.elapsed()) << '\n';
	}
}