Blender  V3.3
BLI_timeit.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
5 #include <chrono>
6 #include <iostream>
7 #include <string>
8 
9 #include "BLI_sys_types.h"
10 
11 namespace blender::timeit {
12 
13 using Clock = std::chrono::steady_clock;
14 using TimePoint = Clock::time_point;
15 using Nanoseconds = std::chrono::nanoseconds;
16 
17 void print_duration(Nanoseconds duration);
18 
19 class ScopedTimer {
20  private:
21  std::string name_;
22  TimePoint start_;
23 
24  public:
25  ScopedTimer(std::string name) : name_(std::move(name))
26  {
27  start_ = Clock::now();
28  }
29 
31  {
32  const TimePoint end = Clock::now();
33  const Nanoseconds duration = end - start_;
34 
35  std::cout << "Timer '" << name_ << "' took ";
36  print_duration(duration);
37  std::cout << '\n';
38  }
39 };
40 
42  private:
43  std::string name_;
44  TimePoint start_;
45 
46  int64_t &total_count_;
47  Nanoseconds &total_time_;
48  Nanoseconds &min_time_;
49 
50  public:
51  ScopedTimerAveraged(std::string name,
52  int64_t &total_count,
53  Nanoseconds &total_time,
54  Nanoseconds &min_time)
55  : name_(std::move(name)),
56  total_count_(total_count),
57  total_time_(total_time),
58  min_time_(min_time)
59  {
60  start_ = Clock::now();
61  }
62 
64 };
65 
66 } // namespace blender::timeit
67 
68 #define SCOPED_TIMER(name) blender::timeit::ScopedTimer scoped_timer(name)
69 
74 #define SCOPED_TIMER_AVERAGED(name) \
75  static int64_t total_count_; \
76  static blender::timeit::Nanoseconds total_time_; \
77  static blender::timeit::Nanoseconds min_time_ = blender::timeit::Nanoseconds::max(); \
78  blender::timeit::ScopedTimerAveraged scoped_timer(name, total_count_, total_time_, min_time_)
ScopedTimerAveraged(std::string name, int64_t &total_count, Nanoseconds &total_time, Nanoseconds &min_time)
Definition: BLI_timeit.hh:51
ScopedTimer(std::string name)
Definition: BLI_timeit.hh:25
Clock::time_point TimePoint
Definition: BLI_timeit.hh:14
void print_duration(Nanoseconds duration)
Definition: timeit.cc:10
std::chrono::nanoseconds Nanoseconds
Definition: BLI_timeit.hh:15
std::chrono::steady_clock Clock
Definition: BLI_timeit.hh:13
__int64 int64_t
Definition: stdint.h:89