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