Annotation of nono/lib/stopwatch.h, revision 1.1.1.2

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.