Last active
July 15, 2020 21:15
-
-
Save vitaut/7ec12070347e4f4ee3f70d64950f7a10 to your computer and use it in GitHub Desktop.
This file contains 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
#include <benchmark/benchmark.h> | |
#include <fmt/compile.h> | |
#include <fmt/os.h> | |
#include <cstdio> | |
#include <fstream> | |
#if 1 //__cpp_lib_concepts>=201907L | |
#include "../fast_io/include/fast_io.h" | |
#include "../fast_io/include/fast_io_device.h" | |
#endif | |
const char* test_str ="Hello, world!"; | |
int num_iters = 1'000'000; | |
void fprintf(benchmark::State& state) { | |
std::unique_ptr<FILE,decltype(std::fclose)*> fp(std::fopen("fprintf.txt","wb"),std::fclose); | |
for (auto s : state) { | |
for (int i = 0; i < num_iters; ++i) { | |
std::fprintf(fp.get(), "%s\n", test_str); | |
} | |
} | |
} | |
BENCHMARK(fprintf); | |
void std_ofstream(benchmark::State& state) { | |
std::ofstream os("ofstream.txt",std::ofstream::binary); | |
for (auto s : state) { | |
for (int i = 0; i < num_iters; ++i) { | |
os << std::string_view(test_str) <<'\n'; | |
} | |
} | |
} | |
BENCHMARK(std_ofstream); | |
void fmt_print(benchmark::State& state) { | |
fmt::direct_buffered_file f("fmt_print.txt", fmt::file::WRONLY | fmt::file::CREATE); | |
for (auto s : state) { | |
for (int i = 0; i < num_iters; ++i) { | |
fmt::print(f, FMT_COMPILE("{}\n"), std::string_view(test_str)); | |
} | |
} | |
} | |
BENCHMARK(fmt_print); | |
void fast_io_println(benchmark::State& state) { | |
fast_io::obuf_file obf("fast_io_println.txt"); | |
for (auto s : state) { | |
for (int i = 0; i < num_iters; ++i) { | |
println(obf, std::string_view(test_str)); | |
} | |
} | |
} | |
BENCHMARK(fast_io_println); | |
BENCHMARK_MAIN(); | |
/* | |
2020-07-11 08:44:11 | |
Running ./file-benchmark | |
Run on (16 X 5000 MHz CPU s) | |
CPU Caches: | |
L1 Data 32K (x8) | |
L1 Instruction 32K (x8) | |
L2 Unified 256K (x8) | |
L3 Unified 16384K (x1) | |
Load Average: 0.56, 0.54, 0.30 | |
---------------------------------------------------------- | |
Benchmark Time CPU Iterations | |
---------------------------------------------------------- | |
fprintf 48174360 ns 48171386 ns 14 | |
std_ofstream 31823293 ns 31821828 ns 21 | |
fmt_print 7883662 ns 7883940 ns 81 | |
fast_io_println 8555129 ns 8555077 ns 81 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Switched to string_view because fast_io prints C strings as pointers.
As you can see {fmt} is ~8% faster than fast_io.