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

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: 
                     37:        // 経過時間を nsec で返す
                     38:        uint64 Elapsed() const {
                     39:                using namespace std::chrono;
                     40:                if (running) {
                     41:                        return duration_cast<nanoseconds>(Now() - epoch + total).count();
                     42:                } else {
                     43:                        return duration_cast<nanoseconds>(total).count();
                     44:                }
                     45:        }
                     46: 
                     47:  private:
                     48:        // 現在時刻を返す
                     49:        steady_clock::time_point Now() const {
                     50:                return steady_clock::now();
                     51:        }
                     52: 
                     53:        // 前回までの経過時間の合計
                     54:        steady_clock::duration total {};
                     55: 
                     56:        // 今回の Start 時刻
                     57:        steady_clock::time_point epoch {};
                     58: 
                     59:        // ストップウォッチが動作中なら true
                     60:        bool running {};
                     61: };

unix.superglobalmegacorp.com

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