From d6a7179ba5ab57819b1cc64a099ebb1b35be5d4e Mon Sep 17 00:00:00 2001 From: Drath Date: Fri, 2 Jan 2026 01:44:06 +0100 Subject: [PATCH] yuge optimization of fastreadnum --- include/utils.hpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/include/utils.hpp b/include/utils.hpp index 926333d..4a722d2 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -1,6 +1,8 @@ #ifndef UTILS_HPP #define UTILS_HPP + +#include #include #include #include @@ -20,15 +22,25 @@ ssize_t readFile(const std::string& path, char* buffer, size_t maxlen); ssize_t readFile(const char* path, char* buffer, size_t maxlen); std::string getFirstDir(const std::string_view& path); -template -inline int fastReadNum(const std::string& path, char (&buf)[N]) { +/* reads a number from file REALLY fast */ +template +inline void fastReadNum(const std::string& path, char (&buf)[N], T& result) { readFile(path, buf, N); - int ret = 0; - std::from_chars(buf, buf + N, ret); - return ret; + std::from_chars(buf, buf + N, result); } -inline std::string pNum(auto num, uint8_t decs = 0) { +/* reads a number slightly slower and divides it */ +template +inline void fastReadNumDiv(const std::string& path, char (&buf)[N], T& result) { + uint64_t temp{}; + fastReadNum(path, buf, temp); + if constexpr (std::is_floating_point_v) + result = temp / static_cast(Divider); + else + result = static_cast(temp / Divider); +} + +inline std::string pNum(const auto num, const uint8_t decs = 0) { return std::format("{:.{}f}", num, decs); }