Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/BLI_timeit.hh
| Show First 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | ScopedTimerAveraged(std::string name, | ||||
| min_time_(min_time) | min_time_(min_time) | ||||
| { | { | ||||
| start_ = Clock::now(); | start_ = Clock::now(); | ||||
| } | } | ||||
| ~ScopedTimerAveraged(); | ~ScopedTimerAveraged(); | ||||
| }; | }; | ||||
| class ScopeTimerTable { | |||||
| private: | |||||
| using key_type = std::string; | |||||
| const std::string name_; | |||||
| /* Print new table each update or only final version. */ | |||||
| const bool re_print_; | |||||
| /* Line-size tabutation for colums. */ | |||||
| const bool alignment_; | |||||
| MultiValueMap<key_type, Nanoseconds> timings_; | |||||
| Vector<key_type> colums_; | |||||
| class Timer { | |||||
| private: | |||||
| const key_type key_; | |||||
| const TimePoint start_; | |||||
| ScopeTimerTable &table_; | |||||
| public: | |||||
| Timer(ScopeTimerTable &table, const key_type key) | |||||
| : key_(std::move(key)), start_(Clock::now()), table_(table) | |||||
| { | |||||
| } | |||||
| ~Timer() | |||||
| { | |||||
| const TimePoint end = Clock::now(); | |||||
| const Nanoseconds duration = end - start_; | |||||
| table_.timings_.add(key_, duration); | |||||
| table_.colums_.append_non_duplicates(std::move(key_)); | |||||
| if (table_.re_print_) { | |||||
| table_.print(); | |||||
| } | |||||
| } | |||||
| }; | |||||
| public: | |||||
| ScopeTimerTable(std::string name, const bool re_print, const bool alignment) | |||||
| : name_(std::move(name)), re_print_(re_print), alignment_(alignment) | |||||
| { | |||||
| } | |||||
| ~ScopeTimerTable() | |||||
| { | |||||
| if (!re_print_) { | |||||
| this->print(); | |||||
| } | |||||
| } | |||||
| Timer new_timer(const key_type key) | |||||
| { | |||||
| return Timer(*this, std::move(key)); | |||||
| } | |||||
| protected: | |||||
| void print() | |||||
| { | |||||
| for (const key_type key : colums_.as_span().drop_back(1)) { | |||||
| std::cout << key << "\t"; | |||||
| } | |||||
| std::cout << colums_.last() << "\n"; | |||||
| int max_row = 0; | |||||
| for (const key_type key : colums_) { | |||||
| max_row = math::max(int(timings_.lookup(key).size()), max_row); | |||||
| } | |||||
| for (const int64_t index : IndexRange(max_row)) { | |||||
| for (const key_type key : colums_) { | |||||
| const Span<Nanoseconds> colume = timings_.lookup(key); | |||||
| if (colume.size() > index) { | |||||
| std::cout << colume[index].count(); | |||||
| std::cout << "\t"; | |||||
| } | |||||
| } | |||||
| std::cout << "\n"; | |||||
| } | |||||
| std::cout << "\n"; | |||||
| } | |||||
| }; | |||||
| } // namespace blender::timeit | } // namespace blender::timeit | ||||
| #define SCOPED_TIMER(name) blender::timeit::ScopedTimer scoped_timer(name) | #define SCOPED_TIMER(name) blender::timeit::ScopedTimer scoped_timer(name) | ||||
| /** | /** | ||||
| * Print the average and minimum runtime of the timer's scope. | * Print the average and minimum runtime of the timer's scope. | ||||
| * \warning This uses static variables, so it is not thread-safe. | * \warning This uses static variables, so it is not thread-safe. | ||||
| */ | */ | ||||
| #define SCOPED_TIMER_AVERAGED(name) \ | #define SCOPED_TIMER_AVERAGED(name) \ | ||||
| static int64_t total_count_; \ | static int64_t total_count_; \ | ||||
| static blender::timeit::Nanoseconds total_time_; \ | static blender::timeit::Nanoseconds total_time_; \ | ||||
| static blender::timeit::Nanoseconds min_time_ = blender::timeit::Nanoseconds::max(); \ | static blender::timeit::Nanoseconds min_time_ = blender::timeit::Nanoseconds::max(); \ | ||||
| blender::timeit::ScopedTimerAveraged scoped_timer(name, total_count_, total_time_, min_time_) | blender::timeit::ScopedTimerAveraged scoped_timer(name, total_count_, total_time_, min_time_) | ||||
| #define SCOPED_TIMER_TABLE(name, re_print, alignment) \ | |||||
| static blender::timeit::ScopeTimerTable table##name("name", re_print, alignment); \ | |||||
| auto TABLE_ADD = table##name.new_timer; | |||||
| No newline at end of file | |||||