From c5afdbddcf97802892f638aa0b2d8c1c6aad9dbc Mon Sep 17 00:00:00 2001 From: gabi Date: Mon, 1 Dec 2014 18:25:42 +0200 Subject: [PATCH] Use fmt::pad for faster formatting of the default format pattern --- .../spdlog/details/pattern_formatter_impl.h | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/include/spdlog/details/pattern_formatter_impl.h b/include/spdlog/details/pattern_formatter_impl.h index aa0c4e8d..786a6e69 100644 --- a/include/spdlog/details/pattern_formatter_impl.h +++ b/include/spdlog/details/pattern_formatter_impl.h @@ -371,18 +371,32 @@ class full_formatter :public flag_formatter { auto duration = msg.time.time_since_epoch(); auto millis = std::chrono::duration_cast(duration).count() % 1000; - msg.formatted.write("[{:d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:03d}] [{}] [{}] ", - msg.tm_time.tm_year + 1900, - msg.tm_time.tm_mon + 1, - msg.tm_time.tm_mday, - msg.tm_time.tm_hour, - msg.tm_time.tm_min, - msg.tm_time.tm_sec, - static_cast(millis), - msg.logger_name, - level::to_str(msg.level)); - msg.formatted.write(msg.raw.str()); + + /* Slower version(while still very fast - about 3.2 million lines/sec), + msg.formatted.write("[{:d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}.{:03d}] [{}] [{}] {} ", + msg.tm_time.tm_year + 1900, + msg.tm_time.tm_mon + 1, + msg.tm_time.tm_mday, + msg.tm_time.tm_hour, + msg.tm_time.tm_min, + msg.tm_time.tm_sec, + static_cast(millis), + msg.logger_name, + level::to_str(msg.level), + msg.raw.str());*/ + + // Faster (albeit uglier) way to format the line (5.6 million lines/sec) + msg.formatted << '[' << msg.tm_time.tm_year + 1900 << '-' + << fmt::pad(msg.tm_time.tm_mon + 1, 2, '0') << '-' + << fmt::pad(msg.tm_time.tm_mday, 2, '0') << ' ' + << fmt::pad(msg.tm_time.tm_hour, 2, '0') << ':' + << fmt::pad(msg.tm_time.tm_min, 2, '0') << ':' + << fmt::pad(msg.tm_time.tm_sec, 2, '0') << '.' + << fmt::pad(static_cast(millis), 3, '0') << "] "; + + msg.formatted << '[' << msg.logger_name << "] [" << level::to_str(msg.level) << "] "; + msg.formatted.write(msg.raw.data(), msg.raw.size()); } }; @@ -429,7 +443,7 @@ inline void spdlog::pattern_formatter::handle_flag(char flag) { switch (flag) { - // logger name + // logger name case 'n': _formatters.push_back(std::unique_ptr(new details::name_formatter())); break;