1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-15 18:57:08 +00:00

Refactor profiler and related classes

This commit is contained in:
sfan5 2024-04-10 23:56:34 +02:00
parent 5a07f5a652
commit 72eeb9fecb
7 changed files with 133 additions and 132 deletions

View file

@ -23,12 +23,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "log.h"
#include <ostream>
TimeTaker::TimeTaker(const std::string &name, u64 *result, TimePrecision prec)
void TimeTaker::start()
{
m_name = name;
m_result = result;
m_precision = prec;
m_time1 = porting::getTime(prec);
m_time1 = porting::getTime(m_precision);
}
u64 TimeTaker::stop(bool quiet)
@ -39,15 +36,8 @@ u64 TimeTaker::stop(bool quiet)
(*m_result) += dtime;
} else {
if (!quiet) {
static const char* const units[] = {
"s" /* PRECISION_SECONDS */,
"ms" /* PRECISION_MILLI */,
"us" /* PRECISION_MICRO */,
"ns" /* PRECISION_NANO */,
};
infostream << m_name << " took "
<< dtime << units[m_precision]
<< std::endl;
<< dtime << TimePrecision_units[m_precision] << std::endl;
}
}
m_running = false;

View file

@ -19,18 +19,43 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include <string>
#include "irrlichttypes.h"
#include "gettime.h"
enum TimePrecision : s8
{
PRECISION_SECONDS,
PRECISION_MILLI,
PRECISION_MICRO,
PRECISION_NANO,
};
constexpr const char *TimePrecision_units[] = {
"s" /* PRECISION_SECONDS */,
"ms" /* PRECISION_MILLI */,
"us" /* PRECISION_MICRO */,
"ns" /* PRECISION_NANO */,
};
/*
TimeTaker
*/
// Note: this class should be kept lightweight
class TimeTaker
{
public:
TimeTaker(const std::string &name, u64 *result=nullptr,
TimePrecision prec=PRECISION_MILLI);
TimeTaker(const std::string &name, u64 *result = nullptr,
TimePrecision prec = PRECISION_MILLI)
{
if (result)
m_result = result;
else
m_name = name;
m_precision = prec;
start();
}
~TimeTaker()
{
@ -42,9 +67,11 @@ public:
u64 getTimerTime();
private:
void start();
std::string m_name;
u64 *m_result = nullptr;
u64 m_time1;
bool m_running = true;
TimePrecision m_precision;
u64 *m_result = nullptr;
};