1 | //===----------------------------------------------------------------------===// |
2 | // DuckDB |
3 | // |
4 | // duckdb/common/profiler.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/common/chrono.hpp" |
12 | #include "duckdb/common/helper.hpp" |
13 | |
14 | namespace duckdb { |
15 | |
16 | //! The profiler can be used to measure elapsed time |
17 | template <typename T> |
18 | class BaseProfiler { |
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 | time_point<T> Tick() const { |
41 | return T::now(); |
42 | } |
43 | time_point<T> start; |
44 | time_point<T> end; |
45 | bool finished = false; |
46 | }; |
47 | |
48 | using Profiler = BaseProfiler<system_clock>; |
49 | |
50 | } // namespace duckdb |
51 | |