Blender V5.0
time.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include <functional>
8
9#include "util/string.h"
10
12
13/* Give current time in seconds in double precision, with good accuracy. */
14
15double time_dt();
16
17/* Sleep for the specified number of seconds. */
18
19void time_sleep(const double t);
20
21/* Fast timer for applications where overhead is critical and some inaccuracy is acceptable.
22 *
23 * On x86, this uses RDTSCP, which also can check which CPU the code runs on, which in turn
24 * allows us to skip measurements where we moved CPU in-between (which might be invalid due
25 * to different clock states between cores and/or misleading due to OS scheduling). Therefore,
26 * we provide last_cpu to time_fast_tick, and it may set it if supported. */
27
28uint64_t time_fast_tick(uint32_t *last_cpu);
30
31/* Scoped timer. */
32
34 public:
35 explicit scoped_timer(double *value = nullptr) : value_(value)
36 {
38 }
39
41 {
42 if (value_ != nullptr) {
43 *value_ = get_time();
44 }
45 }
46
47 double get_start() const
48 {
49 return time_start_;
50 }
51
52 double get_time() const
53 {
54 return time_dt() - time_start_;
55 }
56
57 protected:
58 double *value_;
60};
61
63 public:
69
70 bool lap(uint64_t &delta)
71 {
72 uint32_t new_cpu = 0;
73 uint64_t new_value = time_fast_tick(&new_cpu);
74
75 const bool cpu_consistent = new_cpu == last_cpu;
76 delta = new_value - last_value;
77
78 last_cpu = new_cpu;
79 last_value = new_value;
80
81 return cpu_consistent;
82 }
83
84 protected:
85 uint32_t last_cpu;
87};
88
90 public:
91 using callback_type = std::function<void(double)>;
92
94
96 {
97 if (cb) {
98 cb(timer.get_time());
99 }
100 }
101
102 protected:
105};
106
107/* Make human readable string from time, compatible with Blender metadata. */
108
109string time_human_readable_from_seconds(const double seconds);
110double time_human_readable_to_seconds(const string &time_string);
111
unsigned long long int uint64_t
bool lap(uint64_t &delta)
Definition time.h:70
uint64_t last_value
Definition time.h:86
uint32_t last_cpu
Definition time.h:85
fast_timer()
Definition time.h:64
callback_type cb
Definition time.h:104
scoped_callback_timer(callback_type cb)
Definition time.h:93
scoped_timer timer
Definition time.h:103
std::function< void(double)> callback_type
Definition time.h:91
double * value_
Definition time.h:58
double get_start() const
Definition time.h:47
~scoped_timer()
Definition time.h:40
double get_time() const
Definition time.h:52
double time_start_
Definition time.h:59
scoped_timer(double *value=nullptr)
Definition time.h:35
#define CCL_NAMESPACE_END
uint64_t time_fast_frequency()
Definition time.cpp:135
void time_sleep(const double t)
Definition time.cpp:56
double time_human_readable_to_seconds(const string &time_string)
Definition time.cpp:156
string time_human_readable_from_seconds(const double seconds)
Definition time.cpp:143
CCL_NAMESPACE_BEGIN double time_dt()
Definition time.cpp:47
uint64_t time_fast_tick(uint32_t *last_cpu)
Definition time.cpp:129