proxygen
BaseBenchmark.cpp File Reference
#include <atomic>
#include <glog/logging.h>
#include <folly/Benchmark.h>
#include <folly/gen/Base.h>

Go to the source code of this file.

Classes

struct  FibYielder
 

Functions

static std::atomic< int > testSize (1000)
 
 BENCHMARK (Sum_Basic_NoGen, iters)
 
 BENCHMARK_RELATIVE (Sum_Basic_Gen, iters)
 
 BENCHMARK_DRAW_LINE ()
 
 BENCHMARK (Sum_Vector_NoGen, iters)
 
 BENCHMARK_RELATIVE (Sum_Vector_Gen, iters)
 
 BENCHMARK (Member, iters)
 
 BENCHMARK_RELATIVE (MapMember, iters)
 
 BENCHMARK (Count_Vector_NoGen, iters)
 
 BENCHMARK_RELATIVE (Count_Vector_Gen, iters)
 
 BENCHMARK (Fib_Sum_NoGen, iters)
 
 BENCHMARK_RELATIVE (Fib_Sum_Gen, iters)
 
 BENCHMARK_RELATIVE (Fib_Sum_Gen_Limit, iters)
 
 BENCHMARK_RELATIVE (Fib_Sum_Gen_Static, iters)
 
 BENCHMARK (VirtualGen_0Virtual, iters)
 
 BENCHMARK_RELATIVE (VirtualGen_1Virtual, iters)
 
 BENCHMARK_RELATIVE (VirtualGen_2Virtual, iters)
 
 BENCHMARK_RELATIVE (VirtualGen_3Virtual, iters)
 
 BENCHMARK (Concat_NoGen, iters)
 
 BENCHMARK_RELATIVE (Concat_Gen, iters)
 
 BENCHMARK (Composed_NoGen, iters)
 
 BENCHMARK_RELATIVE (Composed_Gen, iters)
 
 BENCHMARK_RELATIVE (Composed_GenRegular, iters)
 
 BENCHMARK (Sample, iters)
 
int main (int argc, char *argv[])
 

Variables

static vector< int > testVector
 
static vector< vector< int > > testVectorVector
 
static vector< fbstring > strings
 
auto square = [](int x) { return x * x; }
 

Function Documentation

BENCHMARK ( Sum_Basic_NoGen  ,
iters   
)

Definition at line 52 of file BaseBenchmark.cpp.

References folly::doNotOptimizeAway(), i, s, and testSize().

52  {
53  int limit = testSize.load();
54  int s = 0;
55  while (iters--) {
56  for (int i = 0; i < limit; ++i) {
57  s += i;
58  }
59  }
61 }
static std::atomic< int > testSize(1000)
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( Sum_Vector_NoGen  ,
iters   
)

Definition at line 74 of file BaseBenchmark.cpp.

References folly::doNotOptimizeAway(), i, and s.

74  {
75  int s = 0;
76  while (iters--) {
77  for (auto& i : testVector) {
78  s += i;
79  }
80  }
82 }
static vector< int > testVector
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( Member  ,
iters   
)

Definition at line 94 of file BaseBenchmark.cpp.

References folly::doNotOptimizeAway(), folly::gen::from(), folly::gen::member(), s, folly::basic_fbstring< char >::size(), and folly::gen::sum.

94  {
95  int s = 0;
96  while (iters--) {
97  // clang-format off
98  s += from(strings)
100  | sum;
101  // clang-format on
102  }
104 }
std::atomic< int64_t > sum(0)
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
std::enable_if< ExprIsConst< Constness >::value, Map >::type member(Return(Class::*member)() const)
Definition: Base.h:605
static set< string > s
static vector< fbstring > strings
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( Count_Vector_NoGen  ,
iters   
)

Definition at line 120 of file BaseBenchmark.cpp.

References folly::doNotOptimizeAway(), and s.

120  {
121  int s = 0;
122  while (iters--) {
123  for (auto& i : testVector) {
124  if (i * 2 < rand()) {
125  ++s;
126  }
127  }
128  }
130 }
static vector< int > testVector
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( Fib_Sum_NoGen  ,
iters   
)

Definition at line 148 of file BaseBenchmark.cpp.

References a, b, folly::doNotOptimizeAway(), fib(), s, testSize(), and v.

148  {
149  int s = 0;
150  while (iters--) {
151  auto fib = [](size_t limit) -> vector<int> {
152  vector<int> ret;
153  int a = 0;
154  int b = 1;
155  for (size_t i = 0; i < limit; i += 2) {
156  ret.push_back(a += b);
157  ret.push_back(b += a);
158  }
159  return ret;
160  };
161  for (auto& v : fib(testSize.load())) {
162  s += v;
163  }
164  }
166 }
auto v
char b
static std::atomic< int > testSize(1000)
char a
size_t fib(int n)
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( VirtualGen_0Virtual  ,
iters   
)

Definition at line 226 of file BaseBenchmark.cpp.

References folly::doNotOptimizeAway(), folly::gen::map(), s, folly::gen::seq(), square, and folly::gen::sum.

226  {
227  int s = 0;
228  while (iters--) {
229  auto numbers = seq(1, 10000);
230  auto squares = numbers | map(square);
231  auto quads = squares | map(square);
232  s += quads | sum;
233  }
235 }
std::atomic< int64_t > sum(0)
Gen seq(Value first, Value last)
Definition: Base.h:484
static Map map(mapCap)
static set< string > s
auto square
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( Concat_NoGen  ,
iters   
)

Definition at line 272 of file BaseBenchmark.cpp.

References folly::doNotOptimizeAway(), i, and s.

272  {
273  int s = 0;
274  while (iters--) {
275  for (auto& v : testVectorVector) {
276  for (auto& i : v) {
277  s += i;
278  }
279  }
280  }
282 }
static vector< vector< int > > testVectorVector
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( Composed_NoGen  ,
iters   
)

Definition at line 294 of file BaseBenchmark.cpp.

References folly::doNotOptimizeAway(), i, and s.

294  {
295  int s = 0;
296  while (iters--) {
297  for (auto& i : testVector) {
298  s += i * i;
299  }
300  }
302 }
static vector< int > testVector
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( Sample  ,
iters   
)

Definition at line 323 of file BaseBenchmark.cpp.

References folly::doNotOptimizeAway(), s, folly::gen::sample(), folly::gen::seq(), and folly::gen::sum.

323  {
324  size_t s = 0;
325  while (iters--) {
326  auto sampler = seq(1, 10 * 1000 * 1000) | sample(1000);
327  s += (sampler | sum);
328  }
330 }
std::atomic< int64_t > sum(0)
Gen seq(Value first, Value last)
Definition: Base.h:484
detail::Sample< Random > sample(size_t count, Random rng=Random())
Definition: Base-inl.h:2594
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_DRAW_LINE ( )
BENCHMARK_RELATIVE ( Sum_Basic_Gen  ,
iters   
)

Definition at line 63 of file BaseBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::doNotOptimizeAway(), folly::gen::range(), s, folly::gen::sum, and testSize().

63  {
64  int limit = testSize.load();
65  int s = 0;
66  while (iters--) {
67  s += range(0, limit) | sum;
68  }
70 }
std::atomic< int64_t > sum(0)
static std::atomic< int > testSize(1000)
Gen range(Value begin, Value end)
Definition: Base.h:467
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( Sum_Vector_Gen  ,
iters   
)

Definition at line 84 of file BaseBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::doNotOptimizeAway(), folly::gen::from(), s, and folly::gen::sum.

84  {
85  int s = 0;
86  while (iters--) {
87  s += from(testVector) | sum;
88  }
90 }
std::atomic< int64_t > sum(0)
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
static vector< int > testVector
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( MapMember  ,
iters   
)

Definition at line 106 of file BaseBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::doNotOptimizeAway(), folly::gen::from(), folly::gen::map(), s, and folly::gen::sum.

106  {
107  int s = 0;
108  while (iters--) {
109  // clang-format off
110  s += from(strings)
111  | map([](const fbstring& x) { return x.size(); })
112  | sum;
113  // clang-format on
114  }
116 }
Definition: InvokeTest.cpp:58
std::atomic< int64_t > sum(0)
static Map map(mapCap)
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
static set< string > s
basic_fbstring< char > fbstring
Definition: FBString.h:2904
static vector< fbstring > strings
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( Count_Vector_Gen  ,
iters   
)

Definition at line 132 of file BaseBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::gen::count, folly::doNotOptimizeAway(), folly::gen::filter(), folly::gen::from(), and s.

132  {
133  int s = 0;
134  while (iters--) {
135  // clang-format off
136  s += from(testVector)
137  | filter([](int i) {
138  return i * 2 < rand();
139  })
140  | count;
141  // clang-format on
142  }
144 }
PUSHMI_INLINE_VAR constexpr detail::filter_fn filter
Definition: filter.h:75
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
int * count
static vector< int > testVector
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( Fib_Sum_Gen  ,
iters   
)

Definition at line 168 of file BaseBenchmark.cpp.

References a, b, folly::doNotOptimizeAway(), fib(), GENERATOR, s, folly::gen::sum, folly::gen::take(), testSize(), and folly::fibers::yield().

168  {
169  int s = 0;
170  while (iters--) {
171  auto fib = GENERATOR(int) {
172  int a = 0;
173  int b = 1;
174  for (;;) {
175  yield(a += b);
176  yield(b += a);
177  }
178  };
179  // Early stopping implemented with exceptions.
180  s += fib | take(testSize.load()) | sum;
181  }
183 }
std::atomic< int64_t > sum(0)
char b
static std::atomic< int > testSize(1000)
#define GENERATOR(TYPE)
Definition: Base.h:515
char a
size_t fib(int n)
static set< string > s
detail::Take take(Number count)
Definition: Base-inl.h:2582
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( Fib_Sum_Gen_Limit  ,
iters   
)

Definition at line 185 of file BaseBenchmark.cpp.

References a, b, folly::doNotOptimizeAway(), fib(), GENERATOR, s, folly::gen::sum, testSize(), and folly::fibers::yield().

185  {
186  int s = 0;
187  while (iters--) {
188  size_t limit = testSize.load();
189  auto fib = GENERATOR(int) {
190  int a = 0;
191  int b = 1;
192  for (size_t i = 0; i < limit; i += 2) {
193  yield(a += b);
194  yield(b += a);
195  }
196  };
197  // No early stopping.
198  s += fib | sum;
199  }
201 }
std::atomic< int64_t > sum(0)
char b
static std::atomic< int > testSize(1000)
#define GENERATOR(TYPE)
Definition: Base.h:515
char a
size_t fib(int n)
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( Fib_Sum_Gen_Static  ,
iters   
)

Definition at line 215 of file BaseBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::doNotOptimizeAway(), fib(), s, folly::gen::sum, folly::gen::take(), and testSize().

215  {
216  int s = 0;
217  while (iters--) {
218  auto fib = generator<int>(FibYielder());
219  s += fib | take(testSize.load()) | sum;
220  }
222 }
std::atomic< int64_t > sum(0)
static std::atomic< int > testSize(1000)
size_t fib(int n)
static set< string > s
detail::Take take(Number count)
Definition: Base-inl.h:2582
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( VirtualGen_1Virtual  ,
iters   
)

Definition at line 237 of file BaseBenchmark.cpp.

References folly::doNotOptimizeAway(), folly::gen::map(), s, folly::gen::seq(), square, and folly::gen::sum.

237  {
238  int s = 0;
239  while (iters--) {
240  VirtualGen<int> numbers = seq(1, 10000);
241  auto squares = numbers | map(square);
242  auto quads = squares | map(square);
243  s += quads | sum;
244  }
246 }
std::atomic< int64_t > sum(0)
Gen seq(Value first, Value last)
Definition: Base.h:484
static Map map(mapCap)
static set< string > s
auto square
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( VirtualGen_2Virtual  ,
iters   
)

Definition at line 248 of file BaseBenchmark.cpp.

References folly::doNotOptimizeAway(), folly::gen::map(), s, folly::gen::seq(), square, and folly::gen::sum.

248  {
249  int s = 0;
250  while (iters--) {
251  VirtualGen<int> numbers = seq(1, 10000);
252  VirtualGen<int> squares = numbers | map(square);
253  auto quads = squares | map(square);
254  s += quads | sum;
255  }
257 }
std::atomic< int64_t > sum(0)
Gen seq(Value first, Value last)
Definition: Base.h:484
static Map map(mapCap)
static set< string > s
auto square
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( VirtualGen_3Virtual  ,
iters   
)

Definition at line 259 of file BaseBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::doNotOptimizeAway(), folly::gen::map(), s, folly::gen::seq(), square, and folly::gen::sum.

259  {
260  int s = 0;
261  while (iters--) {
262  VirtualGen<int> numbers = seq(1, 10000);
263  VirtualGen<int> squares = numbers | map(square);
264  VirtualGen<int> quads = squares | map(square);
265  s += quads | sum;
266  }
268 }
std::atomic< int64_t > sum(0)
Gen seq(Value first, Value last)
Definition: Base.h:484
static Map map(mapCap)
static set< string > s
auto square
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( Concat_Gen  ,
iters   
)

Definition at line 284 of file BaseBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::doNotOptimizeAway(), folly::gen::from(), folly::gen::rconcat, s, and folly::gen::sum.

284  {
285  int s = 0;
286  while (iters--) {
287  s += from(testVectorVector) | rconcat | sum;
288  }
290 }
std::atomic< int64_t > sum(0)
static vector< vector< int > > testVectorVector
constexpr detail::RangeConcat rconcat
Definition: Base-inl.h:2571
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
static set< string > s
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( Composed_Gen  ,
iters   
)

Definition at line 304 of file BaseBenchmark.cpp.

References folly::doNotOptimizeAway(), folly::gen::from(), folly::gen::map(), s, square, and folly::gen::sum.

304  {
305  int s = 0;
306  auto sumSq = map(square) | sum;
307  while (iters--) {
308  s += from(testVector) | sumSq;
309  }
311 }
std::atomic< int64_t > sum(0)
static Map map(mapCap)
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
static vector< int > testVector
static set< string > s
auto square
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK_RELATIVE ( Composed_GenRegular  ,
iters   
)

Definition at line 313 of file BaseBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), folly::doNotOptimizeAway(), folly::gen::from(), folly::gen::map(), s, square, and folly::gen::sum.

313  {
314  int s = 0;
315  while (iters--) {
316  s += from(testVector) | map(square) | sum;
317  }
319 }
std::atomic< int64_t > sum(0)
static Map map(mapCap)
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
static vector< int > testVector
static set< string > s
auto square
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
int main ( int  argc,
char *  argv[] 
)

Definition at line 367 of file BaseBenchmark.cpp.

References folly::runBenchmarks().

367  {
368  gflags::ParseCommandLineFlags(&argc, &argv, true);
370  return 0;
371 }
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv
static std::atomic<int> testSize ( 1000  )
static

Referenced by BENCHMARK(), and BENCHMARK_RELATIVE().

Variable Documentation

auto square = [](int x) { return x * x; }

Definition at line 50 of file BaseBenchmark.cpp.

Referenced by BENCHMARK(), and BENCHMARK_RELATIVE().

vector<fbstring> strings
static
Initial value:
=
| eachTo<fbstring>()
| as<vector>()
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
static vector< int > testVector

Definition at line 44 of file BaseBenchmark.cpp.

Referenced by testing::gtest_printers_test::AllowsGenericStreamingAndImplicitConversionTemplate< T >::operator bool(), testing::internal::UniversalTersePrinter< wchar_t * >::Print(), TEST(), testing::gtest_printers_test::TEST(), and testString2Integral().

vector<int> testVector
static
Initial value:
=
seq(1, testSize.load())
| mapped([](int) { return rand(); })
| as<vector>()
static std::atomic< int > testSize(1000)
Gen seq(Value first, Value last)
Definition: Base.h:484
Map mapped(Predicate pred=Predicate())
Definition: Base.h:540

Definition at line 33 of file BaseBenchmark.cpp.

vector<vector<int> > testVectorVector
static
Initial value:
=
seq(1, 100)
| map([](int i) {
return seq(1, i) | as<vector>();
})
| as<vector>()
Gen seq(Value first, Value last)
Definition: Base.h:484
static Map map(mapCap)

Definition at line 38 of file BaseBenchmark.cpp.