Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse format w/o "reparsing" it? #269

Closed
ddevienne opened this issue Feb 4, 2016 · 3 comments
Closed

Reuse format w/o "reparsing" it? #269

ddevienne opened this issue Feb 4, 2016 · 3 comments

Comments

@ddevienne
Copy link

With Boost.Format, I can compile the format once, and use it several times.

boost::format fmt("some complex and long format");
for (const auto& item : items) {
  str(fmt % item.arg1 % item.arg2 % ...);
}

Despite CppFormat 2.0 being faster, isn't there an equivalent to the above? Thanks, --DD

@Spartan322
Copy link

There is not a cppformat format object (as far as I'm aware). The original idea was based on the python methods of formatting and thus takes the majority of the original design from it. I don't think there was anything in here inspired by Boost. (at least, I haven't seen a single Boost inspired part, but I'm not 100% sure)

@vitaut
Copy link
Contributor

vitaut commented Feb 4, 2016

There is no equivalent in C++ Format for this. Moreover, I don't think it's worth doing for the following reasons:

  1. Parsing time takes a relatively small fraction of formatting time. Comparing fmt::Writer+std::string to fmt::format performance numbers in http://zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html suggests that it's no more than 7% (actually much less) on fast integer formatting. If you format something more complex like a floating-point number, parsing time will be negligible.
  2. Compiling string in advance adds an overhead because one needs to create a copy of the format string and additional data structures. This will only pay off if you use it many times. I think this is one of the reason Boost.Format is so slow.

C++ Format is faster than Boost.Format, so you can simply do

for (const auto& item : items) {
  fmt::format("some complex and long format", item.arg1, item.arg2, ...);
}

and this will already be faster than the Boost version.

If you want to speed it up, you can avoid std::string construction which, unlike parsing, does take significant portion of time:

fmt::MemoryWriter writer;
for (const auto& item : items) {
  writer.write("some complex and long format", item.arg1, item.arg2, ...);
  writer.c_str(); // Access the formatted string as a C string to avoid std::string allocation
}

To speed this even further you can use the write API:

fmt::MemoryWriter writer;
for (const auto& item : items) {
  writer << "literal text " << item.arg1 << " more literal text " << item.arg2 << ...;
  writer.c_str(); // Access the formatted string as a C string to avoid std::string allocation
}

@vitaut vitaut closed this as completed Feb 4, 2016
@ddevienne
Copy link
Author

thank you for the thorough answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants