/ test / support / timer.h
timer.h
 1  //===----------------------------------------------------------------------===////
 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  #ifndef TIMER_H
10  #define TIMER_H
11  
12  // Define LIBCXXABI_NO_TIMER to disable testing with a timer.
13  #ifndef LIBCXXABI_NO_TIMER
14  
15  #include <chrono>
16  #include <cstdio>
17  
18  class timer
19  {
20      typedef std::chrono::high_resolution_clock Clock;
21      typedef Clock::time_point TimePoint;
22      typedef std::chrono::microseconds MicroSeconds;
23  public:
24      timer() : m_start(Clock::now()) {}
25  
26      timer(timer const &) = delete;
27      timer & operator=(timer const &) = delete;
28  
29      ~timer()
30      {
31          using std::chrono::duration_cast;
32          TimePoint end = Clock::now();
33          MicroSeconds us = duration_cast<MicroSeconds>(end - m_start);
34          std::printf("%d microseconds\n", us.count());
35      }
36  
37  private:
38      TimePoint m_start;
39  };
40  
41  #else /* LIBCXXABI_NO_TIMER */
42  
43  class timer
44  {
45  public:
46      timer() {}
47      timer(timer const &) = delete;
48      timer & operator=(timer const &) = delete;
49      ~timer() {}
50  };
51  
52  #endif /* LIBCXXABI_NO_TIMER */
53  
54  #endif /* TIMER_H */