Skip to content

Instantly share code, notes, and snippets.

@vitaut
Last active September 10, 2021 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitaut/25864fcec78caba0c8dd6e1c17763ab9 to your computer and use it in GitHub Desktop.
Save vitaut/25864fcec78caba0c8dd6e1c17763ab9 to your computer and use it in GitHub Desktop.
#include <benchmark/benchmark.h>
#include <fmt/chrono.h>
#include <fmt/compile.h>
#include <ctime>
static void year_month_day_strftime(benchmark::State& state) {
srand(0);
tm t;
t.tm_year = rand() % 10000 - 1900;
t.tm_mon = rand() % 12 + 1;
t.tm_mday = rand() % 31;
t.tm_hour = rand() % 24;
t.tm_min = rand() % 60;
t.tm_sec = rand() % 60;
for (auto _ : state) {
char buf[10] = {};
std::strftime(buf, 10, "%F", &t);
benchmark::DoNotOptimize(buf);
}
}
BENCHMARK(year_month_day_strftime);
static void year_month_day_fmt(benchmark::State& state) {
srand(0);
tm t;
t.tm_year = rand() % 10000 - 1900;
t.tm_mon = rand() % 12 + 1;
t.tm_mday = rand() % 31;
t.tm_hour = rand() % 24;
t.tm_min = rand() % 60;
t.tm_sec = rand() % 60;
for (auto _ : state) {
char buf[10] = {};
fmt::format_to(buf, FMT_COMPILE("{:%F}"), t);
benchmark::DoNotOptimize(buf);
}
}
BENCHMARK(year_month_day_fmt);
BENCHMARK_MAIN();
---
Before:
2021-09-10 07:56:39
Running ./a.out
Run on (8 X 2300 MHz CPU s)
CPU Caches:
L1 Data 49K (x4)
L1 Instruction 32K (x4)
L2 Unified 524K (x4)
L3 Unified 8388K (x1)
Load Average: 2.92, 3.31, 3.36
------------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------------
year_month_day_strftime 336 ns 336 ns 2002781
year_month_day_fmt 360 ns 360 ns 1992616
After:
% c++ test.cc -I benchmark/include -I include -std=c++17 src/format.cc benchmark/src/libbenchmark.a -DNDEBUG -O3 -march=native && ./a.out
2021-09-10 07:53:40
Running ./a.out
Run on (8 X 2300 MHz CPU s)
CPU Caches:
L1 Data 49K (x4)
L1 Instruction 32K (x4)
L2 Unified 524K (x4)
L3 Unified 8388K (x1)
Load Average: 1.99, 3.82, 3.53
------------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------------
year_month_day_strftime 337 ns 337 ns 2036612
year_month_day_fmt 4.93 ns 4.93 ns 135893304
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment