Add scoped timing

This commit is contained in:
Grayson Riffe 2025-01-30 13:06:50 -06:00
parent ccfbc86f4f
commit a31c7ab55b
6 changed files with 41 additions and 4 deletions

View File

@ -1,5 +1,5 @@
# NF library CMakeLists.txt
add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp" "src/include/nf/EngineConfig.h")
add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp" "src/include/nf/EngineConfig.h" "src/util/util.cpp")
# Use C++20
set_property(TARGET NothinFancy PROPERTY CXX_STANDARD 20)

View File

@ -16,9 +16,11 @@ namespace nf {
SetThreadDescription(GetCurrentThread(), L"NF Main Thread");
SetConsoleTitle(std::format("{} Debug Console - {}", engineStr, gameStr).c_str());
#endif
{
NFTime();
std::this_thread::sleep_for(std::chrono::seconds(1));
NFLog("Test!");
}
std::cin.get();
}

View File

@ -2,3 +2,20 @@
#pragma once
#include "util/log.h"
#ifdef _DEBUG
#define NFTime() ::nf::util::ScopedTimer __scopeTimer(__FUNCSIG__)
#else
#define NFTime()
#endif
namespace nf::util {
class ScopedTimer {
public:
ScopedTimer(const char* funcName);
~ScopedTimer();
private:
const char* m_funcName;
std::chrono::time_point<std::chrono::high_resolution_clock> m_startTime;
};
}

View File

@ -16,6 +16,7 @@ namespace nf::util {
// Mapping of type strings
static std::map<LogType, const char*> s_logTypesMap = {
{LogType::Log, "\x1b[93mLog\x1b[0m"},
{LogType::Timing, "\x1b[92mTiming\x1b[0m"},
{LogType::Error, "\x1b[91mError\x1b[0m"}
};

View File

@ -14,6 +14,7 @@
namespace nf::util {
enum class LogType {
Log,
Timing,
Error
};

View File

@ -0,0 +1,16 @@
// Miscellaneous utilities implementation
#include "pch.h"
#include "util.h"
namespace nf::util {
ScopedTimer::ScopedTimer(const char* funcName)
: m_funcName(funcName)
, m_startTime(std::chrono::high_resolution_clock::now())
{}
ScopedTimer::~ScopedTimer() {
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - m_startTime);
::nf::util::log(std::format("Scope in {} took {:.3f} ms", m_funcName, duration.count() * 1e-3), ::nf::util::LogType::Timing);
}
}