| 1 | /* -*- C++ -*- */ |
| 2 | |
| 3 | /* |
| 4 | |
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
| 6 | |
| 7 | Copyright (C) 2000-2020 by Emery Berger |
| 8 | http://www.emeryberger.com |
| 9 | [email protected] |
| 10 | |
| 11 | Heap Layers is distributed under the terms of the Apache 2.0 license. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | #ifndef HL_TIMER_H |
| 19 | #define HL_TIMER_H |
| 20 | |
| 21 | /** |
| 22 | * @class Timer |
| 23 | * @brief A portable class for high-resolution timing. |
| 24 | * |
| 25 | * This class simplifies timing measurements across a number of platforms. |
| 26 | * |
| 27 | * @code |
| 28 | * Timer t; |
| 29 | * t.start(); |
| 30 | * // do some work |
| 31 | * t.stop(); |
| 32 | * cout << "That took " << (double) t << " seconds." << endl; |
| 33 | * @endcode |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /* Updated to use new C++11 high-resolution timer classes. */ |
| 38 | |
| 39 | #if (__cplusplus < 201103) |
| 40 | #include "timer-old.h" |
| 41 | #else |
| 42 | |
| 43 | #include <chrono> |
| 44 | |
| 45 | namespace HL { |
| 46 | |
| 47 | class Timer { |
| 48 | public: |
| 49 | |
| 50 | Timer() { |
| 51 | _start = _end; |
| 52 | _elapsed = 0.0; |
| 53 | } |
| 54 | |
| 55 | void start() { |
| 56 | _start = std::chrono::high_resolution_clock::now(); |
| 57 | _end = _start; |
| 58 | } |
| 59 | |
| 60 | void stop() { |
| 61 | _end = std::chrono::high_resolution_clock::now(); |
| 62 | std::chrono::duration<double> elapsed_seconds = _end - _start; |
| 63 | _elapsed = elapsed_seconds.count(); |
| 64 | } |
| 65 | |
| 66 | operator double() { |
| 67 | return _elapsed; |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | std::chrono::time_point<std::chrono::high_resolution_clock> _start, _end; |
| 72 | double _elapsed; |
| 73 | }; |
| 74 | |
| 75 | } |
| 76 | |
| 77 | #endif // version of C++ |
| 78 | |
| 79 | #endif |