When using `SCOPED_TIMER` or `SCOPED_TIMER_AVERAGED` the display would switch from ns to ms once the value is over 0.1 ms with a precision of 1. So when the timer value is hovering in the range of 0.1 - 0.2 ms it is not giving any useful information. Fix this by adding another digit to the precision of ms. Pull Request: https://projects.blender.org/blender/blender/pulls/114724
64 lines
1.8 KiB
C++
64 lines
1.8 KiB
C++
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#include "BLI_timeit.hh"
|
|
|
|
#include <algorithm>
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
|
|
namespace blender::timeit {
|
|
|
|
void print_duration(Nanoseconds duration)
|
|
{
|
|
using namespace std::chrono;
|
|
if (duration < microseconds(100)) {
|
|
std::cout << duration.count() << " ns";
|
|
}
|
|
else if (duration < seconds(5)) {
|
|
std::cout << std::fixed << std::setprecision(2) << duration.count() / 1.0e6 << " ms";
|
|
}
|
|
else if (duration > seconds(90)) {
|
|
/* Long durations: print seconds, and also H:m:s */
|
|
const auto dur_hours = duration_cast<hours>(duration);
|
|
const auto dur_mins = duration_cast<minutes>(duration - dur_hours);
|
|
const auto dur_sec = duration_cast<seconds>(duration - dur_hours - dur_mins);
|
|
std::cout << std::fixed << std::setprecision(1) << duration.count() / 1.0e9 << " s ("
|
|
<< dur_hours.count() << "H:" << dur_mins.count() << "m:" << dur_sec.count() << "s)";
|
|
}
|
|
else {
|
|
std::cout << std::fixed << std::setprecision(1) << duration.count() / 1.0e9 << " s";
|
|
}
|
|
}
|
|
|
|
ScopedTimer::~ScopedTimer()
|
|
{
|
|
const TimePoint end = Clock::now();
|
|
const Nanoseconds duration = end - start_;
|
|
|
|
std::cout << "Timer '" << name_ << "' took ";
|
|
print_duration(duration);
|
|
std::cout << '\n';
|
|
}
|
|
|
|
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 << ", Last: ";
|
|
print_duration(duration);
|
|
std::cout << ")\n";
|
|
}
|
|
|
|
} // namespace blender::timeit
|