3

I recently switched from Boost Log to Spdlog for the purported speed advantages.

However, when I compared the runtimes before and after the switch, I'm finding that Boost Log is significantly faster. I'm wondering if I'm doing something wrong with spdlog that is causing the slowdown or if Boost::Log really is the faster solution.

My application is single threaded, compiled with -O3 and -flto, and I'm using Boost 1.69.0 and Spdlog 1.3.1. My runtimes with Boost::Log is about 37s, with Spdlog its 1m22s, and with Spdlog-async its 2m22. My log entries are usually about 80 characters long made with json using nlohmann/json library (which I know isn't the fastest), and I have 3 different loggers that each target their own file.

Boost Setup:

#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/async_frontend.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/parameter/keyword.hpp>

auto backend = boost::make_shared<sinks::text_file_backend>(keywords::file_name = logfile);
backend->auto_flush(true);
auto sink = boost::make_shared<sinks::asynchronous_sink<sinks::text_file_backend>>(backend);
sink->set_filter(expressions::attr<std::string>("Channel") == "logger_name);
boost::log::core::get()->add_sink(sink);

Elsewhere in the code, I declare a logger

boost::log::sources::channel_logger<> logger(boost::log::keywords::channel = "logger_name");
BOOST_LOG(logger) << "some json string";

Spdlog Setup:

#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
auto file_logger = spdlog::basic_logger_st("logger_name", "filename", true);

//I've tried an async logger, but it is even slower!
// auto file_logger = spdlog::basic_logger_st<spdlog::async_factory>("logger_name", "filename", true);

file_logger->set_pattern("%v");

And to use:

std::shared_ptr<spdlog::logger> logger(spdlog::get("logger_name"));
logger->info("some json string");

Any advice would be appreciated.

EDIT

I was able to improve the performance somewhat by adding spdlog::init_thread_pool(1024*1024, 4); before creating any asynchronous loggers, but the performance is still about half of Boost Log.

sheridp
  • 1,386
  • 1
  • 11
  • 24
  • What make you think spdlog should be faster? Anyway, in order to properly test, you need to write a program that does heavy logging and compare the timings. Also, make sure both programs and libraries are compile with optimization on. – Anon Mail May 21 '19 at 19:47
  • From the code you posted, it looks like you perform `spdlog::get()` everytime you do a log operation. I would be surprised if that was actually the case. Can you show us some code that's actually representative of your usage? –  May 21 '19 at 19:47
  • @Frank While I can't post proprietary code, the above code is pretty close. In the real code, the logger is declared as a class member variable and initialized with the `spdlog::get("logger_name");` the `logger->info()` call is used throughout the methods in the class. – sheridp May 21 '19 at 20:56
  • 2
    Something is wrong with your benchmarks. The last time I checked, spdlog was much faster than boost log. Please post the bench code – GabiMe May 29 '19 at 12:32

0 Answers0