mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: Replace C-style TICC/TOCC macros with C++ RAII implementation. (#11857)
Usage is more robust, and will always include timing when the TicToc object goes out of scope.
This commit is contained in:
parent
691c628b07
commit
c7d5cedc7b
64
src/debug.h
64
src/debug.h
|
@ -63,44 +63,38 @@ std::string GetDebugString();
|
||||||
/* Shorter form for passing filename and linenumber */
|
/* Shorter form for passing filename and linenumber */
|
||||||
#define FILE_LINE __FILE__, __LINE__
|
#define FILE_LINE __FILE__, __LINE__
|
||||||
|
|
||||||
/**
|
/** TicToc profiling.
|
||||||
* Used for profiling.
|
|
||||||
*
|
|
||||||
* Usage:
|
* Usage:
|
||||||
* TIC();
|
* static TicToc::State state("A name", 1);
|
||||||
* --Do your code--
|
* TicToc tt(state);
|
||||||
* TOC("A name", 1);
|
* --Do your code--
|
||||||
*
|
*/
|
||||||
* When you run the TIC() / TOC() multiple times, you can increase the '1'
|
struct TicToc {
|
||||||
* to only display average stats every N values. Some things to know:
|
/** Persistent state for TicToc profiling. */
|
||||||
*
|
struct State {
|
||||||
* for (int i = 0; i < 5; i++) {
|
const std::string_view name;
|
||||||
* TIC();
|
const uint32_t max_count;
|
||||||
* --Do your code--
|
uint32_t count = 0;
|
||||||
* TOC("A name", 5);
|
uint64_t chrono_sum = 0;
|
||||||
* }
|
|
||||||
*
|
|
||||||
* Is the correct usage for multiple TIC() / TOC() calls.
|
|
||||||
*
|
|
||||||
* TIC() / TOC() creates its own block, so make sure not the mangle
|
|
||||||
* it with another block.
|
|
||||||
*
|
|
||||||
* The output is counted in microseconds. Mainly useful for local optimisations.
|
|
||||||
**/
|
|
||||||
#define TIC() {\
|
|
||||||
auto _start_ = std::chrono::high_resolution_clock::now();\
|
|
||||||
static uint64_t _sum_ = 0;\
|
|
||||||
static uint32_t _i_ = 0;
|
|
||||||
|
|
||||||
#define TOC(str, _count_)\
|
constexpr State(std::string_view name, uint32_t max_count) : name(name), max_count(max_count) { }
|
||||||
_sum_ += (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - _start_)).count();\
|
};
|
||||||
if (++_i_ == _count_) {\
|
|
||||||
Debug(misc, 0, "[{}] {} us [avg: {:.1f} us]", str, _sum_, _sum_/(double)_i_);\
|
|
||||||
_i_ = 0;\
|
|
||||||
_sum_ = 0;\
|
|
||||||
}\
|
|
||||||
}
|
|
||||||
|
|
||||||
|
State &state;
|
||||||
|
std::chrono::high_resolution_clock::time_point chrono_start; ///< real time count.
|
||||||
|
|
||||||
|
inline TicToc(State &state) : state(state), chrono_start(std::chrono::high_resolution_clock::now()) { }
|
||||||
|
|
||||||
|
inline ~TicToc()
|
||||||
|
{
|
||||||
|
this->state.chrono_sum += (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - this->chrono_start)).count();
|
||||||
|
if (++this->state.count == this->state.max_count) {
|
||||||
|
Debug(misc, 0, "[{}] {} us [avg: {:.1f} us]", this->state.name, this->state.chrono_sum, this->state.chrono_sum / static_cast<double>(this->state.count));
|
||||||
|
this->state.count = 0;
|
||||||
|
this->state.chrono_sum = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void ShowInfoI(const std::string &str);
|
void ShowInfoI(const std::string &str);
|
||||||
#define ShowInfo(format_string, ...) ShowInfoI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))
|
#define ShowInfo(format_string, ...) ShowInfoI(fmt::format(FMT_STRING(format_string), ## __VA_ARGS__))
|
||||||
|
|
Loading…
Reference in New Issue