Skip to content

why does fmt::format_to_n perform so much worse than fmt::format_to #3484

Description

@mentalmap

fmt Version:

10.0.0

Benchmark:

#include <benchmark/benchmark.h>
#include <fmt/chrono.h>
#include <fmt/compile.h>
#include <fmt/core.h>
#include <fmt/format.h>

static void BM_format_to(benchmark::State &state) {
  char out[1024] = {0};
  auto format = FMT_COMPILE("{} - {} - {} - {} - {} - {} - {} - {} - {} - {}");

  for (auto _ : state) {
    fmt::format_to(out, format, "abcdef", 12345, "abcdef", 12345, "abcdef", 12345, "abcdef", 12345,
                   "abcdef", 12345);
  }
}
BENCHMARK(BM_format_to);

static void BM_format_to_n(benchmark::State &state) {
  char out[1024] = {0};
  auto format = FMT_COMPILE("{} - {} - {} - {} - {} - {} - {} - {} - {} - {}");

  for (auto _ : state) {
    fmt::format_to_n(out, sizeof(out), format, "abcdef", 12345, "abcdef", 12345, "abcdef", 12345,
                     "abcdef", 12345, "abcdef", 12345);
  }
}
BENCHMARK(BM_format_to_n);

BENCHMARK_MAIN();

Result:

Running ./benchmark_format_to.fmt10
Run on (16 X 2595.12 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x8)
  L1 Instruction 32 KiB (x8)
  L2 Unified 4096 KiB (x8)
  L3 Unified 16384 KiB (x2)
Load Average: 1.17, 1.10, 1.59
---------------------------------------------------------
Benchmark               Time             CPU   Iterations
---------------------------------------------------------
BM_format_to         78.3 ns         78.3 ns      8849844
BM_format_to_n        564 ns          564 ns      1242982

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions