C++11 Timer

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 00:30   25   0
// timer.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include <chrono>

using namespace std;
using namespace std::chrono;

class Timer
{
public:
 Timer():m_begin(high_resolution_clock::now()){}
 void reset() { m_begin = high_resolution_clock::now(); }

 // 默认输出毫秒
 template<typename Duration=microseconds>
 int64_t elapsed() const
 {
  return duration_cast<Duration>(high_resolution_clock::now() - m_begin).count();
 }
 // 微秒
 int64_t elapsed_micro() const
 {
  return elapsed<microseconds>();
 }
 // 纳秒
 int64_t elapsed_nano()const
 {
  return elapsed<nanoseconds>();
 }

 // 秒
 int64_t elapsed_seconds()const
 {
  return elapsed<seconds>();
 }

 int64_t elapsed_minutes()const
 {
  return elapsed<minutes>();
 }

 int64_t elasped_hours()const
 {
  return elapsed<hours>();
 }

private:
 time_point<high_resolution_clock> m_begin;
};
void fun()
{
 cout << "hello word" << endl;
}
int main()
{
 Timer t;
 fun();
 
 cout << t.elapsed() <<endl;
 cout << t.elapsed_micro() << endl;
 cout << t.elapsed_nano() << endl;
 cout << t.elapsed_seconds() << endl;
 cout << t.elapsed_minutes() << endl;
 cout << t.elasped_hours() << endl;

}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP