This is useful to save time manually averaging many timing results. The minimum is included because often it can be more stable than an average, and it can help to expose calls from other contexts with lower times that would make the average useless. Differential Revision: https://developer.blender.org/D14417
39 lines
874 B
C++
39 lines
874 B
C++
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#include "BLI_timeit.hh"
|
|
|
|
#include <algorithm>
|
|
|
|
namespace blender::timeit {
|
|
|
|
void print_duration(Nanoseconds duration)
|
|
{
|
|
if (duration < std::chrono::microseconds(100)) {
|
|
std::cout << duration.count() << " ns";
|
|
}
|
|
else if (duration < std::chrono::seconds(5)) {
|
|
std::cout << duration.count() / 1.0e6 << " ms";
|
|
}
|
|
else {
|
|
std::cout << duration.count() / 1.0e9 << " s";
|
|
}
|
|
}
|
|
|
|
ScopedTimerAveraged::~ScopedTimerAveraged()
|
|
{
|
|
const TimePoint end = Clock::now();
|
|
const Nanoseconds duration = end - start_;
|
|
|
|
total_count_++;
|
|
total_time_ += duration;
|
|
min_time_ = std::min(duration, min_time_);
|
|
|
|
std::cout << "Timer '" << name_ << "': (Average: ";
|
|
print_duration(total_time_ / total_count_);
|
|
std::cout << ", Min: ";
|
|
print_duration(min_time_);
|
|
std::cout << ")\n";
|
|
}
|
|
|
|
} // namespace blender::timeit
|