Blender V5.3
BLI_timeit.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include <chrono>
12#include <optional>
13#include <string>
14
15#include "BLI_sys_types.hh"
16
17namespace blender::timeit {
18
19using Clock = std::chrono::steady_clock;
20using TimePoint = Clock::time_point;
21using Nanoseconds = std::chrono::nanoseconds;
22
23void print_duration(Nanoseconds duration);
24
26 private:
27 std::string name_;
28 TimePoint start_;
29
30 public:
31 ScopedTimer(std::string name) : name_(std::move(name))
32 {
33 start_ = Clock::now();
34 }
35
37};
38
40 private:
41 std::string name_;
42 TimePoint start_;
43 Nanoseconds rolling_average_ = Nanoseconds();
44
45 int64_t &total_count_;
46 Nanoseconds &total_time_;
47 Nanoseconds &min_time_;
48 std::optional<int64_t> window_size_;
49 std::optional<int64_t> nth_samples_report_;
50
51 public:
52 ScopedTimerAveraged(std::string name,
53 int64_t &total_count,
54 Nanoseconds &total_time,
55 Nanoseconds &min_time,
56 const std::optional<int64_t> window_size,
57 const std::optional<int64_t> nth_samples_report);
58
60};
61
62} // namespace blender::timeit
63
64#define SCOPED_TIMER(name) timeit::ScopedTimer scoped_timer(name)
65
70#define SCOPED_TIMER_AVERAGED(name) \
71 static int64_t total_count_; \
72 static timeit::Nanoseconds total_time_; \
73 static timeit::Nanoseconds min_time_ = timeit::Nanoseconds::max(); \
74 timeit::ScopedTimerAveraged scoped_timer( \
75 name, total_count_, total_time_, min_time_, std::nullopt, std::nullopt)
76
81#define SCOPED_TIMER_AVERAGED_NTH_SAMPLES(name, nth_samples) \
82 static int64_t total_count_; \
83 static timeit::Nanoseconds total_time_; \
84 static timeit::Nanoseconds min_time_ = timeit::Nanoseconds::max(); \
85 timeit::ScopedTimerAveraged scoped_timer( \
86 name, total_count_, total_time_, min_time_, std::nullopt, nth_samples)
87
92#define SCOPED_TIMER_ROLLING_AVERAGED(name, window_size) \
93 static int64_t total_count_; \
94 static timeit::Nanoseconds total_time_; \
95 static timeit::Nanoseconds min_time_ = timeit::Nanoseconds::max(); \
96 timeit::ScopedTimerAveraged scoped_timer( \
97 name, total_count_, total_time_, min_time_, window_size, std::nullopt)
ScopedTimerAveraged(std::string name, int64_t &total_count, Nanoseconds &total_time, Nanoseconds &min_time, const std::optional< int64_t > window_size, const std::optional< int64_t > nth_samples_report)
Definition timeit.cc:64
ScopedTimer(std::string name)
Definition BLI_timeit.hh:31
std::chrono::nanoseconds Nanoseconds
Definition BLI_timeit.hh:21
Clock::time_point TimePoint
Definition BLI_timeit.hh:20
std::chrono::steady_clock Clock
Definition BLI_timeit.hh:19
void print_duration(Nanoseconds duration)
Definition timeit.cc:45
__int64 int64_t
Definition BKE_text.h:90