-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
optimize append #2164
optimize append #2164
Conversation
optimize loop
Thanks for the PR. Could you post a benchmark demonstrating the effect of this change in the comments here? |
I've performed the following elementary benchmark: gcc (g++-10 (Homebrew GCC 10.2.0_4) 10.2.0):
here's the benchmark code: #include <benchmark/benchmark.h>
#include <fmt/format.h>
#include <fmt/printf.h>
void one_format_argument(benchmark::State& state) {
const auto format_string = "{}" + std::string(state.range(0), 'x');
const auto test_output = fmt::format(format_string, 'a');
if (test_output != "a" + std::string(state.range(0), 'x')) {
throw std::runtime_error("wrong");
}
for (auto _ : state) {
benchmark::DoNotOptimize(fmt::format(format_string, 'a', 'a'));
}
}
void five_format_arguments(benchmark::State& state) {
auto format_string = std::string();
for (int i = 0; i < 5; ++i) {
format_string += "{}" + std::string(state.range(0), 'x');
}
const auto test_output = fmt::format(format_string, 'a', 'a', 'a', 'a', 'a');
if (state.range(0) == 0 && test_output != std::string(5, 'a')) {
throw std::runtime_error("wrong");
}
for (auto _ : state) {
benchmark::DoNotOptimize(
fmt::format(format_string, 'a', 'a', 'a', 'a', 'a'));
}
}
void ten_format_arguments(benchmark::State& state) {
auto format_string = std::string();
for (int i = 0; i < 10; ++i) {
format_string += "{}" + std::string(state.range(0), 'x');
}
const auto test_output = fmt::format(format_string, 'a', 'a', 'a', 'a', 'a',
'a', 'a', 'a', 'a', 'a');
if (state.range(0) == 0 && test_output != std::string(10, 'a')) {
throw std::runtime_error("wrong");
}
for (auto _ : state) {
benchmark::DoNotOptimize(fmt::format(format_string, 'a', 'a', 'a', 'a', 'a',
'a', 'a', 'a', 'a', 'a'));
}
}
BENCHMARK(one_format_argument)->Arg(0)->Arg(10)->Arg(100)->Arg(1000);
BENCHMARK(five_format_arguments)->Arg(0)->Arg(10)->Arg(100)->Arg(1000);
BENCHMARK(ten_format_arguments)->Arg(0)->Arg(10)->Arg(100)->Arg(1000);
BENCHMARK_MAIN(); |
Thank you both! |
Avoid one unneeded empty loop pass, e.g. with
std::string message = fmt::sprintf("The answer is %d", 42);
(mostly if % placeholder is at the beginning or end of the string to be formatted)
I agree that my contributions are licensed under the {fmt} license, and agree to future changes to the licensing.