|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2021 nono project
4: // Licensed under nono-license.txt
5: //
6:
1.1.1.3 ! root 7: //
! 8: // Stopwatch
! 9: //
! 10:
1.1 root 11: #pragma once
12:
13: #include "header.h"
14: #include <chrono>
15:
16: // .Net の Stopwatch みたいなもの。
17: // Start() と Restart() が分かりづらいけど、もうそっくり真似しておく
18: class Stopwatch
19: {
20: using steady_clock = std::chrono::steady_clock;
21:
22: public:
23: // ストップウォッチを再開する
24: void Start() {
25: running = true;
26: epoch = Now();
27: }
28:
29: // ストップウォッチを一時停止する
30: void Stop() {
31: total += Now() - epoch;
32: running = false;
33: }
34:
35: // 経過時間をリセットした上で、ストップウォッチを再開(開始)する
36: void Restart() {
37: total = steady_clock::duration::zero();
38: Start();
39: }
40:
1.1.1.2 root 41: // ストップウォッチを停止して、経過時間をリセットする
42: void Reset() {
43: Stop();
44: total = steady_clock::duration::zero();
45: }
46:
1.1 root 47: // 経過時間を nsec で返す
48: uint64 Elapsed() const {
49: using namespace std::chrono;
50: if (running) {
51: return duration_cast<nanoseconds>(Now() - epoch + total).count();
52: } else {
53: return duration_cast<nanoseconds>(total).count();
54: }
55: }
56:
57: private:
58: // 現在時刻を返す
59: steady_clock::time_point Now() const {
60: return steady_clock::now();
61: }
62:
63: // 前回までの経過時間の合計
64: steady_clock::duration total {};
65:
66: // 今回の Start 時刻
67: steady_clock::time_point epoch {};
68:
69: // ストップウォッチが動作中なら true
70: bool running {};
71: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.