Page 1 of 1

Replacing boost::format with fmt

Posted: 28 Oct 2017, 08:24
by psi29a
In looking to de-boost, I stumbled across boost::string(boost::format( that happens quite a bit in our codebase, it does utf8/wide %s string formatting.

Apparently, thanks to benchmarks, it shows that boost is the slowest of the string formatting options. Introducing FMT, a C++11 library that apparently used by many projects for this specific purpose and is super fast.

https://github.com/fmtlib/fmt

I've already tested it here and have it in a branch, but I wanted to know how you guys felt about it? We can either add it to our 3rd party libs that we carry with us, or add as a dependency for packagers.

Benchmarks:
Library Method Run Time, s
EGLIBC 2.19 printf 1.30
libstdc++ 4.8.2 std::ostream 1.85
fmt 1.0 fmt::print 1.42
tinyformat 2.0.1 tfm::printf 2.25
Boost Format 1.54 boost::format 9.94

Re: Replacing boost::format with fmt

Posted: 28 Oct 2017, 09:36
by raevol
I'm not sure what I am talking about here, but why not use std::ostream ? Seems only a little slower than fmt (and still way faster than boost), but isn't external? I may be missing something big here though...

Re: Replacing boost::format with fmt

Posted: 28 Oct 2017, 13:29
by psi29a
Pure convenience, instead of having to juggle a string buffer and worry about it... I can just use:
auto result = fmt::sprintf("something %s", 'cool'); // <-- 'something cool' in result that is std::string
^-- very wow, very python ;)

otherwise I would have to do things like described here:
https://stackoverflow.com/questions/369 ... tring-in-c

Re: Replacing boost::format with fmt

Posted: 28 Oct 2017, 16:53
by scrawl
Performance is irrelevant here for a function that's barely used.

Most of current uses seem to be either replacing %s with a given string or just formatting a number (not even surrounded by a string). This should be trivial to implement ourselves. Don't see the need to add a new dependency.

Re: Replacing boost::format with fmt

Posted: 28 Oct 2017, 19:49
by Chris
With C++11 we also have access to std::to_string now, and given rvalue move semantics and small-string optimizations, should be fairly good performance-wise, as far as string manipulation goes. fmt might be something to look at if these things aren't enough, but I'd first try to go through and see what we can do with standard functions first.