1 | //===----------------------------------------------------------------------===// |
2 | // DuckDB |
3 | // |
4 | // duckdb/common/profiler.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/common/helper.hpp" |
12 | |
13 | #include <chrono> |
14 | |
15 | namespace duckdb { |
16 | |
17 | //! The profiler can be used to measure elapsed time |
18 | class Profiler { |
19 | public: |
20 | //! Starts the timer |
21 | void Start() { |
22 | finished = false; |
23 | start = Tick(); |
24 | } |
25 | //! Finishes timing |
26 | void End() { |
27 | end = Tick(); |
28 | finished = true; |
29 | } |
30 | |
31 | //! Returns the elapsed time in seconds. If End() has been called, returns |
32 | //! the total elapsed time. Otherwise returns how far along the timer is |
33 | //! right now. |
34 | double Elapsed() const { |
35 | auto _end = finished ? end : Tick(); |
36 | return std::chrono::duration_cast<std::chrono::duration<double>>(_end - start).count(); |
37 | } |
38 | |
39 | private: |
40 | std::chrono::time_point<std::chrono::system_clock> Tick() const { |
41 | return std::chrono::system_clock::now(); |
42 | } |
43 | std::chrono::time_point<std::chrono::system_clock> start; |
44 | std::chrono::time_point<std::chrono::system_clock> end; |
45 | bool finished = false; |
46 | }; |
47 | } // namespace duckdb |
48 | |