This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/source/blender/blenlib/intern/timeit.cc
Hans Goudey 7ef3a1a6e6 BLI: Add utility for tacking average and min runtime
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
2022-03-23 23:35:23 -05:00

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