Commit: 7ef3a1a6e6e7495c94076c2dea332aa40ec09db1 Author: Hans Goudey Date: Wed Mar 23 23:35:23 2022 -0500 Branches: master https://developer.blender.org/rB7ef3a1a6e6e7495c94076c2dea332aa40ec09db1
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 =================================================================== M source/blender/blenlib/BLI_timeit.hh M source/blender/blenlib/intern/timeit.cc =================================================================== diff --git a/source/blender/blenlib/BLI_timeit.hh b/source/blender/blenlib/BLI_timeit.hh index 31e1b5d2a03..2c89c8dd7ac 100644 --- a/source/blender/blenlib/BLI_timeit.hh +++ b/source/blender/blenlib/BLI_timeit.hh @@ -38,6 +38,41 @@ class ScopedTimer { } }; +class ScopedTimerAveraged { + private: + std::string name_; + TimePoint start_; + + int64_t &total_count_; + Nanoseconds &total_time_; + Nanoseconds &min_time_; + + public: + ScopedTimerAveraged(std::string name, + int64_t &total_count, + Nanoseconds &total_time, + Nanoseconds &min_time) + : name_(std::move(name)), + total_count_(total_count), + total_time_(total_time), + min_time_(min_time) + { + start_ = Clock::now(); + } + + ~ScopedTimerAveraged(); +}; + } // namespace blender::timeit #define SCOPED_TIMER(name) blender::timeit::ScopedTimer scoped_timer(name) + +/** + * Print the average and minumum runtime of the timer's scope. + * \warning This uses static variables, so it is not thread-safe. + */ +#define SCOPED_TIMER_AVERAGED(name) \ + static int64_t total_count_; \ + static blender::timeit::Nanoseconds total_time_; \ + static blender::timeit::Nanoseconds min_time_ = blender::timeit::Nanoseconds::max(); \ + blender::timeit::ScopedTimerAveraged scoped_timer(name, total_count_, total_time_, min_time_) diff --git a/source/blender/blenlib/intern/timeit.cc b/source/blender/blenlib/intern/timeit.cc index 2dcfe2e6ab1..f11f9c4ad94 100644 --- a/source/blender/blenlib/intern/timeit.cc +++ b/source/blender/blenlib/intern/timeit.cc @@ -2,6 +2,8 @@ #include "BLI_timeit.hh" +#include <algorithm> + namespace blender::timeit { void print_duration(Nanoseconds duration) @@ -17,4 +19,20 @@ void print_duration(Nanoseconds duration) } } +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 _______________________________________________ Bf-blender-cvs mailing list [email protected] List details, subscription details or unsubscribe: https://lists.blender.org/mailman/listinfo/bf-blender-cvs
