C++11计算时间差

#pragma once

#include 
#include 

using namespace std::chrono;

class Timer {
public:
	using s = std::ratio<1, 1>;
	using ms = std::ratio<1, 1000>;
	using us = std::ratio<1, 1000000>;
	using ns = std::ratio<1, 1000000000>;

public:
	Timer() : tpStart(high_resolution_clock::now()), tpStop(tpStart) {}

public:
	void start() { tpStart = high_resolution_clock::now(); }
	void restart() { tpStart = high_resolution_clock::now(); }
	void stop() { tpStop = high_resolution_clock::now(); }

	template 
	auto delta() const { return duration(high_resolution_clock::now() - tpStart).count(); }
	
	template 
	auto delta_restart() {
		auto ts = duration(high_resolution_clock::now() - tpStart).count();
		start();
		return ts;
	}

	template 
	auto stop_delta() { stop(); return duration(tpStop - tpStart).count(); }

	template 
	auto stop_delta_start() {
		stop();
		auto ts = duration(tpStop - tpStart).count();
		start();
		return ts;
	}

private:
	time_point tpStart;
	time_point tpStop;
};

 

你可能感兴趣的:(C++)