Blender V5.0
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.h"
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
50 public:
52 int64_t &total_count,
54 Nanoseconds &min_time,
55 const std::optional<int64_t> window_size)
56 : name_(std::move(name)),
57 total_count_(total_count),
58 total_time_(total_time),
59 min_time_(min_time),
60 window_size_(window_size)
61 {
62 start_ = Clock::now();
63 }
64
66};
67
68} // namespace blender::timeit
69
70#define SCOPED_TIMER(name) blender::timeit::ScopedTimer scoped_timer(name)
71
76#define SCOPED_TIMER_AVERAGED(name) \
77 static int64_t total_count_; \
78 static blender::timeit::Nanoseconds total_time_; \
79 static blender::timeit::Nanoseconds min_time_ = blender::timeit::Nanoseconds::max(); \
80 blender::timeit::ScopedTimerAveraged scoped_timer( \
81 name, total_count_, total_time_, min_time_, std::nullopt)
82
87#define SCOPED_TIMER_ROLLING_AVERAGED(name, window_size) \
88 static int64_t total_count_; \
89 static blender::timeit::Nanoseconds total_time_; \
90 static blender::timeit::Nanoseconds min_time_ = blender::timeit::Nanoseconds::max(); \
91 blender::timeit::ScopedTimerAveraged scoped_timer( \
92 name, total_count_, total_time_, min_time_, window_size)
long long int int64_t
ScopedTimerAveraged(std::string name, int64_t &total_count, Nanoseconds &total_time, Nanoseconds &min_time, const std::optional< int64_t > window_size)
Definition BLI_timeit.hh:51
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
const char * name
double total_time