1 | //===-- Timer.cpp --------------------------------------------------------===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "Timer.h" |
10 | #include "src/__support/macros/config.h" |
11 | |
12 | #include <chrono> |
13 | #include <fstream> |
14 | |
15 | namespace LIBC_NAMESPACE_DECL { |
16 | namespace testing { |
17 | |
18 | struct TimerImplementation { |
19 | std::chrono::high_resolution_clock::time_point Start; |
20 | std::chrono::high_resolution_clock::time_point End; |
21 | }; |
22 | |
23 | Timer::Timer() : Impl(new TimerImplementation) {} |
24 | |
25 | Timer::~Timer() { delete reinterpret_cast<TimerImplementation *>(Impl); } |
26 | |
27 | void Timer::start() { |
28 | auto T = reinterpret_cast<TimerImplementation *>(Impl); |
29 | T->Start = std::chrono::high_resolution_clock::now(); |
30 | } |
31 | |
32 | void Timer::stop() { |
33 | auto T = reinterpret_cast<TimerImplementation *>(Impl); |
34 | T->End = std::chrono::high_resolution_clock::now(); |
35 | } |
36 | |
37 | uint64_t Timer::nanoseconds() const { |
38 | auto T = reinterpret_cast<TimerImplementation *>(Impl); |
39 | return std::chrono::nanoseconds(T->End - T->Start).count(); |
40 | } |
41 | |
42 | } // namespace testing |
43 | } // namespace LIBC_NAMESPACE_DECL |
44 |