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 | |
11 | #include <chrono> |
12 | #include <fstream> |
13 | |
14 | namespace LIBC_NAMESPACE { |
15 | namespace testing { |
16 | |
17 | struct TimerImplementation { |
18 | std::chrono::high_resolution_clock::time_point Start; |
19 | std::chrono::high_resolution_clock::time_point End; |
20 | }; |
21 | |
22 | Timer::Timer() : Impl(new TimerImplementation) {} |
23 | |
24 | Timer::~Timer() { delete reinterpret_cast<TimerImplementation *>(Impl); } |
25 | |
26 | void Timer::start() { |
27 | auto T = reinterpret_cast<TimerImplementation *>(Impl); |
28 | T->Start = std::chrono::high_resolution_clock::now(); |
29 | } |
30 | |
31 | void Timer::stop() { |
32 | auto T = reinterpret_cast<TimerImplementation *>(Impl); |
33 | T->End = std::chrono::high_resolution_clock::now(); |
34 | } |
35 | |
36 | uint64_t Timer::nanoseconds() const { |
37 | auto T = reinterpret_cast<TimerImplementation *>(Impl); |
38 | return std::chrono::nanoseconds(T->End - T->Start).count(); |
39 | } |
40 | |
41 | } // namespace testing |
42 | } // namespace LIBC_NAMESPACE |
43 |