Skip to content

Instantly share code, notes, and snippets.

@EricWF
Created February 21, 2018 12:35
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 EricWF/0553b45cac931f8bef003d1d08261abb to your computer and use it in GitHub Desktop.
Save EricWF/0553b45cac931f8bef003d1d08261abb to your computer and use it in GitHub Desktop.
#include "benchmark/benchmark.h"
#include "GenerateInput.hpp"
#include "test_iterators.h"
#include <ciso646>
#ifdef _LIBCPP_VERSION
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
static const size_t TestNumInputs = 1024;
template <class GenInputs>
void BM_PathConstructString(benchmark::State &st, GenInputs gen) {
using fs::path;
const auto in = gen(st.range(0));
path PP;
for (auto& Part : in)
PP /= Part;
benchmark::DoNotOptimize(PP.native().data());
while (st.KeepRunning()) {
const path P(PP.native());
benchmark::DoNotOptimize(P.native().data());
}
st.SetComplexityN(st.range(0));
}
BENCHMARK_CAPTURE(BM_PathConstructString, large_string,
getRandomStringInputs)->Range(8, TestNumInputs)->Complexity();
template <class GenInputs>
void BM_PathConstructCStr(benchmark::State &st, GenInputs gen) {
using fs::path;
const auto in = gen(st.range(0));
path PP;
for (auto& Part : in)
PP /= Part;
benchmark::DoNotOptimize(PP.native().data());
while (st.KeepRunning()) {
const path P(PP.native().c_str());
benchmark::DoNotOptimize(P.native().data());
}
}
BENCHMARK_CAPTURE(BM_PathConstructCStr, large_string,
getRandomStringInputs)->Arg(TestNumInputs);
template <template <class...> class ItType, class GenInputs>
void BM_PathConstructIter(benchmark::State &st, GenInputs gen) {
using fs::path;
using Iter = ItType<std::string::const_iterator>;
const auto in = gen(st.range(0));
path PP;
for (auto& Part : in)
PP /= Part;
auto Start = Iter(PP.native().begin());
auto End = Iter(PP.native().end());
benchmark::DoNotOptimize(PP.native().data());
benchmark::DoNotOptimize(Start);
benchmark::DoNotOptimize(End);
while (st.KeepRunning()) {
const path P(Start, End);
benchmark::DoNotOptimize(P.native().data());
}
st.SetComplexityN(st.range(0));
}
template <class GenInputs>
void BM_PathConstructInputIter(benchmark::State &st, GenInputs gen) {
BM_PathConstructIter<input_iterator>(st, gen);
}
template <class GenInputs>
void BM_PathConstructForwardIter(benchmark::State &st, GenInputs gen) {
BM_PathConstructIter<forward_iterator>(st, gen);
}
BENCHMARK_CAPTURE(BM_PathConstructInputIter, large_string,
getRandomStringInputs)->Range(8, TestNumInputs)->Complexity();
BENCHMARK_CAPTURE(BM_PathConstructForwardIter, large_string,
getRandomStringInputs)->Range(8, TestNumInputs)->Complexity();
template <class GenInputs>
void BM_PathIterateMultipleTimes(benchmark::State &st, GenInputs gen) {
using fs::path;
const auto in = gen(st.range(0));
path PP;
for (auto& Part : in)
PP /= Part;
benchmark::DoNotOptimize(PP.native().data());
while (st.KeepRunning()) {
for (auto &E : PP) {
benchmark::DoNotOptimize(E.native().data());
}
benchmark::ClobberMemory();
}
st.SetComplexityN(st.range(0));
}
BENCHMARK_CAPTURE(BM_PathIterateMultipleTimes, iterate_elements,
getRandomStringInputs)->Range(8, TestNumInputs)->Complexity();
template <class GenInputs>
void BM_PathIterateOnce(benchmark::State &st, GenInputs gen) {
using fs::path;
const auto in = gen(st.range(0));
path PP;
for (auto& Part : in)
PP /= Part;
benchmark::DoNotOptimize(PP.native().data());
while (st.KeepRunning()) {
const path P = PP.native();
for (auto &E : P) {
benchmark::DoNotOptimize(E.native().data());
}
benchmark::ClobberMemory();
}
st.SetComplexityN(st.range(0));
}
BENCHMARK_CAPTURE(BM_PathIterateOnce, iterate_elements,
getRandomStringInputs)->Range(8, TestNumInputs)->Complexity();
template <class GenInputs>
void BM_PathIterateOnceBackwards(benchmark::State &st, GenInputs gen) {
using fs::path;
const auto in = gen(st.range(0));
path PP;
for (auto& Part : in)
PP /= Part;
benchmark::DoNotOptimize(PP.native().data());
while (st.KeepRunning()) {
const path P = PP.native();
const auto B = P.begin();
auto I = P.end();
while (I != B) {
--I;
benchmark::DoNotOptimize(*I);
}
benchmark::DoNotOptimize(*I);
}
}
BENCHMARK_CAPTURE(BM_PathIterateOnceBackwards, iterate_elements,
getRandomStringInputs)->Arg(TestNumInputs);
static fs::path getRandomPaths(int NumParts, int PathLen) {
fs::path Result;
while (NumParts--) {
std::string Part = getRandomString(PathLen);
Result /= Part;
}
return Result;
}
template <class GenInput>
void BM_LexicallyNormal(benchmark::State &st, GenInput gen, size_t PathLen) {
using fs::path;
auto In = gen(st.range(0), PathLen);
benchmark::DoNotOptimize(&In);
while (st.KeepRunning()) {
benchmark::DoNotOptimize(In.lexically_normal());
}
st.SetComplexityN(st.range(0));
}
BENCHMARK_CAPTURE(BM_LexicallyNormal, small_path,
getRandomPaths, /*PathLen*/5)->RangeMultiplier(2)->Range(2, 256)->Complexity();
BENCHMARK_CAPTURE(BM_LexicallyNormal, large_path,
getRandomPaths, /*PathLen*/32)->RangeMultiplier(2)->Range(2, 256)->Complexity();
BENCHMARK_MAIN();
2018-02-21 05:22:37
Run on (8 X 4026.61 MHz CPU s)
CPU Caches:
L1 Data 16K (x8)
L1 Instruction 64K (x8)
L2 Unified 2048K (x8)
L3 Unified 8192K (x1)
-----------------------------------------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------------------------------------
BM_PathConstructString/large_string/8 595 ns 595 ns 1310181
BM_PathConstructString/large_string/64 4639 ns 4637 ns 137229
BM_PathConstructString/large_string/512 34397 ns 34399 ns 20218
BM_PathConstructString/large_string/1024 144434 ns 144428 ns 4984
BM_PathConstructString_BigO 0.14 N^2 0.14 N^2
BM_PathConstructString_RMS 5 % 5 %
BM_PathConstructCStr/large_string/1024 709560 ns 709522 ns 996
BM_PathConstructInputIter/large_string/8 60155 ns 60121 ns 12637
BM_PathConstructInputIter/large_string/64 449996 ns 449978 ns 1559
BM_PathConstructInputIter/large_string/512 3839594 ns 3839120 ns 195
BM_PathConstructInputIter/large_string/1024 7720814 ns 7719608 ns 83
BM_PathConstructInputIter_BigO 7530.17 N 7529.04 N
BM_PathConstructInputIter_RMS 1 % 1 %
BM_PathConstructForwardIter/large_string/8 592 ns 592 ns 1100919
BM_PathConstructForwardIter/large_string/64 4896 ns 4896 ns 142998
BM_PathConstructForwardIter/large_string/512 34292 ns 34292 ns 20155
BM_PathConstructForwardIter/large_string/1024 139676 ns 139669 ns 5119
BM_PathConstructForwardIter_BigO 0.13 N^2 0.13 N^2
BM_PathConstructForwardIter_RMS 5 % 5 %
BM_PathIterateMultipleTimes/iterate_elements/8 10058 ns 10052 ns 73783
BM_PathIterateMultipleTimes/iterate_elements/64 85630 ns 85634 ns 8547
BM_PathIterateMultipleTimes/iterate_elements/512 674583 ns 674651 ns 1010
BM_PathIterateMultipleTimes/iterate_elements/1024 1359777 ns 1359632 ns 487
BM_PathIterateMultipleTimes_BigO 1325.87 N 1325.78 N
BM_PathIterateMultipleTimes_RMS 0 % 0 %
BM_PathIterateOnce/iterate_elements/8 10677 ns 10677 ns 59787
BM_PathIterateOnce/iterate_elements/64 84684 ns 84681 ns 8492
BM_PathIterateOnce/iterate_elements/512 725862 ns 725834 ns 930
BM_PathIterateOnce/iterate_elements/1024 1507564 ns 1507396 ns 468
BM_PathIterateOnce_BigO 1460.89 N 1460.75 N
BM_PathIterateOnce_RMS 2 % 2 %
BM_PathIterateOnceBackwards/iterate_elements/1024 1458941 ns 1458772 ns 466
BM_LexicallyNormal/small_path/2 199 ns 199 ns 3404080
BM_LexicallyNormal/small_path/4 353 ns 353 ns 2167342
BM_LexicallyNormal/small_path/8 517 ns 517 ns 1248640
BM_LexicallyNormal/small_path/16 829 ns 829 ns 799838
BM_LexicallyNormal/small_path/32 1482 ns 1482 ns 469220
BM_LexicallyNormal/small_path/64 2678 ns 2678 ns 249848
BM_LexicallyNormal/small_path/128 5214 ns 5214 ns 129129
BM_LexicallyNormal/small_path/256 10294 ns 10294 ns 71563
BM_LexicallyNormal_BigO 40.52 N 40.52 N
BM_LexicallyNormal_RMS 5 % 5 %
BM_LexicallyNormal/large_path/2 315 ns 315 ns 2243690
BM_LexicallyNormal/large_path/4 487 ns 487 ns 1468072
BM_LexicallyNormal/large_path/8 791 ns 791 ns 930532
BM_LexicallyNormal/large_path/16 1391 ns 1391 ns 504102
BM_LexicallyNormal/large_path/32 2535 ns 2534 ns 269693
BM_LexicallyNormal/large_path/64 5065 ns 5065 ns 100000
BM_LexicallyNormal/large_path/128 9778 ns 9779 ns 81113
BM_LexicallyNormal/large_path/256 20694 ns 20694 ns 34544
BM_LexicallyNormal_BigO 79.95 N 79.95 N
BM_LexicallyNormal_RMS 4 % 4 %
2018-02-21 05:23:13
Run on (8 X 4026.61 MHz CPU s)
CPU Caches:
L1 Data 16K (x8)
L1 Instruction 64K (x8)
L2 Unified 2048K (x8)
L3 Unified 8192K (x1)
-----------------------------------------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------------------------------------
BM_PathConstructString/large_string/8 24627 ns 24632 ns 28546
BM_PathConstructString/large_string/64 199100 ns 199067 ns 3430
BM_PathConstructString/large_string/512 1642854 ns 1642723 ns 422
BM_PathConstructString/large_string/1024 3248404 ns 3248446 ns 203
BM_PathConstructString_BigO 3179.34 N 3179.32 N
BM_PathConstructString_RMS 1 % 1 %
BM_PathConstructCStr/large_string/1024 3455381 ns 3455371 ns 194
BM_PathConstructInputIter/large_string/8 41167 ns 41154 ns 16924
BM_PathConstructInputIter/large_string/64 325551 ns 325564 ns 2102
BM_PathConstructInputIter/large_string/512 2615165 ns 2615153 ns 265
BM_PathConstructInputIter/large_string/1024 5479601 ns 5479318 ns 132
BM_PathConstructInputIter_BigO 5301.81 N 5301.58 N
BM_PathConstructInputIter_RMS 3 % 3 %
BM_PathConstructForwardIter/large_string/8 24112 ns 24113 ns 29785
BM_PathConstructForwardIter/large_string/64 193741 ns 193703 ns 3635
BM_PathConstructForwardIter/large_string/512 1597046 ns 1596926 ns 442
BM_PathConstructForwardIter/large_string/1024 3334841 ns 3334835 ns 215
BM_PathConstructForwardIter_BigO 3228.55 N 3228.50 N
BM_PathConstructForwardIter_RMS 2 % 3 %
BM_PathIterateMultipleTimes/iterate_elements/8 20 ns 20 ns 35147652
BM_PathIterateMultipleTimes/iterate_elements/64 145 ns 145 ns 4972452
BM_PathIterateMultipleTimes/iterate_elements/512 1443 ns 1443 ns 494141
BM_PathIterateMultipleTimes/iterate_elements/1024 2882 ns 2880 ns 241212
BM_PathIterateMultipleTimes_BigO 2.81 N 2.81 N
BM_PathIterateMultipleTimes_RMS 2 % 2 %
BM_PathIterateOnce/iterate_elements/8 23816 ns 23821 ns 28165
BM_PathIterateOnce/iterate_elements/64 193957 ns 193941 ns 3636
BM_PathIterateOnce/iterate_elements/512 1652396 ns 1651248 ns 450
BM_PathIterateOnce/iterate_elements/1024 3338505 ns 3337323 ns 213
BM_PathIterateOnce_BigO 3252.97 N 3251.60 N
BM_PathIterateOnce_RMS 1 % 1 %
BM_PathIterateOnceBackwards/iterate_elements/1024 3377812 ns 3362867 ns 209
BM_LexicallyNormal/small_path/2 402 ns 402 ns 1751521
BM_LexicallyNormal/small_path/4 1159 ns 1159 ns 627876
BM_LexicallyNormal/small_path/8 3112 ns 3111 ns 233878
BM_LexicallyNormal/small_path/16 9516 ns 9511 ns 72720
BM_LexicallyNormal/small_path/32 30323 ns 30272 ns 23228
BM_LexicallyNormal/small_path/64 103220 ns 103220 ns 7149
BM_LexicallyNormal/small_path/128 386833 ns 386735 ns 1806
BM_LexicallyNormal/small_path/256 1438367 ns 1437967 ns 481
BM_LexicallyNormal_BigO 22.06 N^2 22.05 N^2
BM_LexicallyNormal_RMS 4 % 4 %
BM_LexicallyNormal/large_path/2 983 ns 983 ns 762585
BM_LexicallyNormal/large_path/4 2606 ns 2609 ns 268216
BM_LexicallyNormal/large_path/8 8423 ns 8423 ns 85020
BM_LexicallyNormal/large_path/16 25803 ns 25804 ns 26518
BM_LexicallyNormal/large_path/32 87593 ns 87593 ns 8336
BM_LexicallyNormal/large_path/64 315061 ns 315034 ns 2227
BM_LexicallyNormal/large_path/128 1207811 ns 1207620 ns 570
BM_LexicallyNormal/large_path/256 5001623 ns 5003782 ns 146
BM_LexicallyNormal_BigO 76.17 N^2 76.20 N^2
BM_LexicallyNormal_RMS 2 % 2 %
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment