optimize append - #2164
Merged
Merged
optimize append#2164
Conversation
optimize loop
Contributor
|
Thanks for the PR. Could you post a benchmark demonstrating the effect of this change in the comments here? |
Contributor
|
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(); |
Contributor
|
Thank you both! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.