X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2Fcontainer%2Ftest%2FForeachBenchmark.cpp;h=a3e810eb5c905135ef173c5263e27e67deb98434;hp=5b741afb35b0e7f759587a9b363122fcf4d87a52;hb=66c782bb73c911a99dc7e41f8aa9659515e3a20b;hpb=6cc78c358adeb78727a4b2cb60808d9ab1891851 diff --git a/folly/container/test/ForeachBenchmark.cpp b/folly/container/test/ForeachBenchmark.cpp index 5b741afb..a3e810eb 100644 --- a/folly/container/test/ForeachBenchmark.cpp +++ b/folly/container/test/ForeachBenchmark.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2017 Facebook, Inc. + * Copyright 2017-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,12 +14,14 @@ * limitations under the License. */ -#include +#include +#include #include -#include - -#include +#include +#include +#include +#include using namespace folly; using namespace folly::detail; @@ -31,10 +33,14 @@ using namespace folly::detail; // iter->second as is, without assigning to local variables. // 3. Use FOR_EACH_KV loop to iterate through the map. -std::map bmMap; // For use in benchmarks below. +// For use in benchmarks below. +std::map bmMap; std::vector vec_one; std::vector vec_two; +// Smallest type to isolate iteration overhead. +std::vector vec_char; + void setupBenchmark(size_t iters) { bmMap.clear(); for (size_t i = 0; i < iters; ++i) { @@ -47,6 +53,12 @@ void setupBenchmark(size_t iters) { vec_two.resize(iters); } +void setupCharVecBenchmark(size_t iters) { + vec_char.resize(iters); + std::generate( + vec_char.begin(), vec_char.end(), [] { return Random::rand32(128); }); +} + BENCHMARK(ForEachFunctionNoAssign, iters) { BenchmarkSuspender suspender; @@ -330,13 +342,61 @@ BENCHMARK(ForEachRangeR, iters) { doNotOptimizeAway(sum); } -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - gflags::ParseCommandLineFlags(&argc, &argv, true); - auto r = RUN_ALL_TESTS(); - if (r) { - return r; +BENCHMARK(CharVecForRange, iters) { + BENCHMARK_SUSPEND { + setupCharVecBenchmark(iters); + } + size_t sum = 0; + for (auto& c : vec_char) { + sum += c; + } + doNotOptimizeAway(sum); +} + +BENCHMARK(CharVecForRangeExplicitIndex, iters) { + BENCHMARK_SUSPEND { + setupCharVecBenchmark(iters); + } + size_t sum = 0; + size_t index = 0; + for (auto& c : vec_char) { + sum += c * index; + ++index; + } + doNotOptimizeAway(sum); +} + +BENCHMARK(CharVecForEach, iters) { + BENCHMARK_SUSPEND { + setupCharVecBenchmark(iters); + } + size_t sum = 0; + folly::for_each(vec_char, [&](auto& c) { sum += c; }); + doNotOptimizeAway(sum); +} + +BENCHMARK(CharVecForEachIndex, iters) { + BENCHMARK_SUSPEND { + setupCharVecBenchmark(iters); + } + size_t sum = 0; + folly::for_each(vec_char, [&](auto& c, auto index) { sum += c * index; }); + doNotOptimizeAway(sum); +} + +BENCHMARK(CharVecForRangeEnumerate, iters) { + BENCHMARK_SUSPEND { + setupCharVecBenchmark(iters); + } + size_t sum = 0; + for (auto it : enumerate(vec_char)) { + sum += *it * it.index; } + doNotOptimizeAway(sum); +} + +int main(int argc, char** argv) { + folly::init(&argc, &argv); runBenchmarks(); return 0; }