| 1 | // Scintilla source code edit control | 
|---|
| 2 | /** @file ElapsedPeriod.h | 
|---|
| 3 | ** Encapsulate C++ <chrono> to simplify use. | 
|---|
| 4 | **/ | 
|---|
| 5 | // Copyright 2018 by Neil Hodgson <neilh@scintilla.org> | 
|---|
| 6 | // The License.txt file describes the conditions under which this software may be distributed. | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef ELAPSEDPERIOD_H | 
|---|
| 9 | #define ELAPSEDPERIOD_H | 
|---|
| 10 |  | 
|---|
| 11 | namespace Scintilla::Internal { | 
|---|
| 12 |  | 
|---|
| 13 | // Simplified access to high precision timing. | 
|---|
| 14 | class ElapsedPeriod { | 
|---|
| 15 | using ElapsedClock = std::chrono::steady_clock; | 
|---|
| 16 | ElapsedClock::time_point tp; | 
|---|
| 17 | public: | 
|---|
| 18 | /// Capture the moment | 
|---|
| 19 | ElapsedPeriod() noexcept : tp(ElapsedClock::now()) { | 
|---|
| 20 | } | 
|---|
| 21 | /// Return duration as floating point seconds | 
|---|
| 22 | double Duration(bool reset=false) noexcept { | 
|---|
| 23 | const ElapsedClock::time_point tpNow = ElapsedClock::now(); | 
|---|
| 24 | const std::chrono::duration<double> duration = | 
|---|
| 25 | std::chrono::duration_cast<std::chrono::duration<double>>(tpNow - tp); | 
|---|
| 26 | if (reset) { | 
|---|
| 27 | tp = tpNow; | 
|---|
| 28 | } | 
|---|
| 29 | return duration.count(); | 
|---|
| 30 | } | 
|---|
| 31 | }; | 
|---|
| 32 |  | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | #endif | 
|---|
| 36 |  | 
|---|