proxygen
ForeachBenchmark.cpp File Reference
#include <algorithm>
#include <map>
#include <folly/Benchmark.h>
#include <folly/Random.h>
#include <folly/container/Enumerate.h>
#include <folly/container/Foreach.h>
#include <folly/init/Init.h>

Go to the source code of this file.

Functions

void setupBenchmark (size_t iters)
 
void setupCharVecBenchmark (size_t iters)
 
 BENCHMARK (ForEachFunctionNoAssign, iters)
 
 BENCHMARK (StdForEachFunctionNoAssign, iters)
 
 BENCHMARK (RangeBasedForLoopNoAssign, iters)
 
 BENCHMARK (ManualLoopNoAssign, iters)
 
 BENCHMARK (ForEachFunctionAssign, iters)
 
 BENCHMARK (StdForEachFunctionAssign, iters)
 
 BENCHMARK (RangeBasedForLoopAssign, iters)
 
 BENCHMARK (ManualLoopAssign, iters)
 
 BENCHMARK (ForEachFunctionNoAssignWithIndexManipulation, iters)
 
 BENCHMARK (StdForEachFunctionNoAssignWithIndexManipulation, iters)
 
 BENCHMARK (RangeBasedForLoopNoAssignWithIndexManipulation, iters)
 
 BENCHMARK (ForEachFunctionFetch, iters)
 
 BENCHMARK (StdForEachFunctionFetch, iters)
 
 BENCHMARK (ForLoopFetch, iters)
 
 BENCHMARK (ForEachKVNoMacroAssign, iters)
 
 BENCHMARK (ForEachKVNoMacroNoAssign, iters)
 
 BENCHMARK (ForEachKVMacro, iters)
 
 BENCHMARK (ForEachManual, iters)
 
 BENCHMARK (ForEachRange, iters)
 
 BENCHMARK (ForEachDescendingManual, iters)
 
 BENCHMARK (ForEachRangeR, iters)
 
 BENCHMARK (CharVecForRange, iters)
 
 BENCHMARK (CharVecForRangeExplicitIndex, iters)
 
 BENCHMARK (CharVecForEach, iters)
 
 BENCHMARK (CharVecForEachIndex, iters)
 
 BENCHMARK (CharVecForRangeEnumerate, iters)
 
int main (int argc, char **argv)
 

Variables

std::map< int, std::stringbmMap
 
std::vector< int > vec_one
 
std::vector< int > vec_two
 
std::vector< char > vec_char
 

Function Documentation

BENCHMARK ( ForEachFunctionNoAssign  ,
iters   
)

Definition at line 62 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::doNotOptimizeAway(), folly::for_each(), setupBenchmark(), and string.

62  {
63  BenchmarkSuspender suspender;
64 
65  int sumKeys = 0;
66  std::string sumValues;
67  setupBenchmark(iters);
68 
69  suspender.dismissing([&]() {
70  folly::for_each(bmMap, [&](auto& key_val_pair) {
71  sumKeys += key_val_pair.first;
72  sumValues += key_val_pair.second;
73  });
74  doNotOptimizeAway(sumKeys);
75  });
76 }
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( StdForEachFunctionNoAssign  ,
iters   
)

Definition at line 78 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::doNotOptimizeAway(), for_each(), setupBenchmark(), and string.

78  {
79  BenchmarkSuspender suspender;
80 
81  int sumKeys = 0;
82  std::string sumValues;
83  setupBenchmark(iters);
84 
85  suspender.dismissing([&]() {
86  std::for_each(bmMap.begin(), bmMap.end(), [&](auto& key_val_pair) {
87  sumKeys += key_val_pair.first;
88  sumValues += key_val_pair.second;
89  });
90  doNotOptimizeAway(sumKeys);
91  });
92 }
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
void for_each(T const &range, Function< void(typename T::value_type const &) const > const &func)
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( RangeBasedForLoopNoAssign  ,
iters   
)

Definition at line 94 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::doNotOptimizeAway(), setupBenchmark(), and string.

94  {
95  BenchmarkSuspender suspender;
96  int sumKeys = 0;
97  std::string sumValues;
98  setupBenchmark(iters);
99 
100  suspender.dismissing([&]() {
101  for (auto& key_val_pair : bmMap) {
102  sumKeys += key_val_pair.first;
103  sumValues += key_val_pair.second;
104  }
105  doNotOptimizeAway(sumKeys);
106  });
107 }
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( ManualLoopNoAssign  ,
iters   
)

Definition at line 109 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::doNotOptimizeAway(), setupBenchmark(), and string.

109  {
110  BenchmarkSuspender suspender;
111 
112  int sumKeys = 0;
113  std::string sumValues;
114  setupBenchmark(iters);
115 
116  suspender.dismissing([&]() {
117  for (auto iter = bmMap.begin(); iter != bmMap.end(); ++iter) {
118  sumKeys += iter->first;
119  sumValues += iter->second;
120  }
121  doNotOptimizeAway(sumKeys);
122  });
123 }
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( ForEachFunctionAssign  ,
iters   
)

Definition at line 125 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::for_each(), k, setupBenchmark(), string, and v.

125  {
126  BenchmarkSuspender suspender;
127 
128  int sumKeys = 0;
129  std::string sumValues;
130  setupBenchmark(iters);
131 
132  suspender.dismissing([&]() {
133  folly::for_each(bmMap, [&](auto& key_val_pair) {
134  const int k = key_val_pair.first;
135  const std::string v = key_val_pair.second;
136  sumKeys += k;
137  sumValues += v;
138  });
139  });
140 }
auto v
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
KeyT k
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
BENCHMARK ( StdForEachFunctionAssign  ,
iters   
)

Definition at line 142 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), for_each(), k, setupBenchmark(), string, and v.

142  {
143  BenchmarkSuspender suspender;
144 
145  int sumKeys = 0;
146  std::string sumValues;
147  setupBenchmark(iters);
148 
149  suspender.dismissing([&]() {
150  std::for_each(bmMap.begin(), bmMap.end(), [&](auto& key_val_pair) {
151  const int k = key_val_pair.first;
152  const std::string v = key_val_pair.second;
153  sumKeys += k;
154  sumValues += v;
155  });
156  });
157 }
auto v
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
void for_each(T const &range, Function< void(typename T::value_type const &) const > const &func)
KeyT k
BENCHMARK ( RangeBasedForLoopAssign  ,
iters   
)

Definition at line 159 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), k, setupBenchmark(), string, and v.

159  {
160  BenchmarkSuspender suspender;
161 
162  int sumKeys = 0;
163  std::string sumValues;
164  setupBenchmark(iters);
165 
166  suspender.dismissing([&]() {
167  for (auto& key_val_pair : bmMap) {
168  const int k = key_val_pair.first;
169  const std::string v = key_val_pair.second;
170  sumKeys += k;
171  sumValues += v;
172  }
173  });
174 }
auto v
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
KeyT k
BENCHMARK ( ManualLoopAssign  ,
iters   
)

Definition at line 176 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), k, setupBenchmark(), string, and v.

176  {
177  BenchmarkSuspender suspender;
178 
179  int sumKeys = 0;
180  std::string sumValues;
181  setupBenchmark(iters);
182 
183  suspender.dismissing([&]() {
184  for (auto iter = bmMap.begin(); iter != bmMap.end(); ++iter) {
185  const int k = iter->first;
186  const std::string v = iter->second;
187  sumKeys += k;
188  sumValues += v;
189  }
190  });
191 }
auto v
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
KeyT k
BENCHMARK ( ForEachFunctionNoAssignWithIndexManipulation  ,
iters   
)

Definition at line 193 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::for_each(), setupBenchmark(), and string.

193  {
194  BenchmarkSuspender suspender;
195 
196  int sumKeys = 0;
197  std::string sumValues;
198  setupBenchmark(iters);
199 
200  suspender.dismissing([&]() {
201  folly::for_each(bmMap, [&](auto& key_val_pair, auto index) {
202  sumKeys += key_val_pair.first;
203  sumValues += key_val_pair.second;
204  sumValues += index;
205  });
206  });
207 }
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
BENCHMARK ( StdForEachFunctionNoAssignWithIndexManipulation  ,
iters   
)

Definition at line 209 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), for_each(), setupBenchmark(), and string.

209  {
210  BenchmarkSuspender suspender;
211 
212  int sumKeys = 0;
213  std::string sumValues;
214  setupBenchmark(iters);
215 
216  suspender.dismissing([&]() {
217  auto index = std::size_t{0};
218  std::for_each(bmMap.begin(), bmMap.end(), [&](auto& key_val_pair) {
219  sumKeys += key_val_pair.first;
220  sumValues += key_val_pair.second;
221  sumValues += index;
222  ++index;
223  });
224  });
225 }
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
void for_each(T const &range, Function< void(typename T::value_type const &) const > const &func)
BENCHMARK ( RangeBasedForLoopNoAssignWithIndexManipulation  ,
iters   
)

Definition at line 227 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), setupBenchmark(), and string.

227  {
228  BenchmarkSuspender suspender;
229 
230  int sumKeys = 0;
231  std::string sumValues;
232  setupBenchmark(iters);
233 
234  suspender.dismissing([&]() {
235  auto index = std::size_t{0};
236  for (auto& key_val_pair : bmMap) {
237  sumKeys += key_val_pair.first;
238  sumValues += key_val_pair.second;
239  sumValues += index;
240  }
241  });
242 }
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
const char * string
Definition: Conv.cpp:212
BENCHMARK ( ForEachFunctionFetch  ,
iters   
)

Definition at line 244 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), folly::fetch(), folly::for_each(), and setupBenchmark().

244  {
245  BenchmarkSuspender suspender;
246  setupBenchmark(iters);
247 
248  suspender.dismissing([&]() {
249  folly::for_each(bmMap, [&](auto& key_val_pair, auto index) {
250  folly::fetch(vec_one, index) = key_val_pair.first;
251  });
252  });
253 }
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
std::vector< int > vec_one
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
decltype(auto) FOLLY_CPP14_CONSTEXPR fetch(Sequence &&sequence, Index &&index)
Definition: Foreach-inl.h:322
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
BENCHMARK ( StdForEachFunctionFetch  ,
iters   
)

Definition at line 255 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), for_each(), and setupBenchmark().

255  {
256  BenchmarkSuspender suspender;
257  setupBenchmark(iters);
258 
259  suspender.dismissing([&]() {
260  auto index = std::size_t{0};
261  std::for_each(bmMap.begin(), bmMap.end(), [&](auto& key_val_pair) {
262  *(vec_one.begin() + index++) = key_val_pair.first;
263  });
264  });
265 }
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
std::vector< int > vec_one
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
void for_each(T const &range, Function< void(typename T::value_type const &) const > const &func)
BENCHMARK ( ForLoopFetch  ,
iters   
)

Definition at line 267 of file ForeachBenchmark.cpp.

References folly::BenchmarkSuspender::dismissing(), and setupBenchmark().

267  {
268  BenchmarkSuspender suspender;
269  setupBenchmark(iters);
270 
271  suspender.dismissing([&]() {
272  auto index = std::size_t{0};
273  for (auto& key_val_pair : bmMap) {
274  *(vec_one.begin() + index++) = key_val_pair.first;
275  }
276  });
277 }
std::map< int, std::string > bmMap
void setupBenchmark(size_t iters)
std::vector< int > vec_one
auto dismissing(F f) -> invoke_result_t< F >
Definition: Benchmark.h:130
BENCHMARK ( ForEachKVNoMacroAssign  ,
iters   
)

Definition at line 279 of file ForeachBenchmark.cpp.

References BENCHMARK_SUSPEND, FOR_EACH, k, setupBenchmark(), string, and v.

279  {
280  int sumKeys = 0;
281  std::string sumValues;
282 
284  setupBenchmark(iters);
285  }
286 
287  FOR_EACH (iter, bmMap) {
288  const int k = iter->first;
289  const std::string v = iter->second;
290  sumKeys += k;
291  sumValues += v;
292  }
293 }
auto v
std::map< int, std::string > bmMap
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
void setupBenchmark(size_t iters)
#define FOR_EACH(i, c)
Definition: Foreach.h:143
const char * string
Definition: Conv.cpp:212
KeyT k
BENCHMARK ( ForEachKVNoMacroNoAssign  ,
iters   
)

Definition at line 295 of file ForeachBenchmark.cpp.

References BENCHMARK_SUSPEND, FOR_EACH, setupBenchmark(), and string.

295  {
296  int sumKeys = 0;
297  std::string sumValues;
298 
300  setupBenchmark(iters);
301  }
302 
303  FOR_EACH (iter, bmMap) {
304  sumKeys += iter->first;
305  sumValues += iter->second;
306  }
307 }
std::map< int, std::string > bmMap
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
void setupBenchmark(size_t iters)
#define FOR_EACH(i, c)
Definition: Foreach.h:143
const char * string
Definition: Conv.cpp:212
BENCHMARK ( ForEachKVMacro  ,
iters   
)

Definition at line 309 of file ForeachBenchmark.cpp.

References BENCHMARK_SUSPEND, FOR_EACH_KV, k, setupBenchmark(), string, and v.

309  {
310  int sumKeys = 0;
311  std::string sumValues;
312 
314  setupBenchmark(iters);
315  }
316 
317  FOR_EACH_KV (k, v, bmMap) {
318  sumKeys += k;
319  sumValues += v;
320  }
321 }
#define FOR_EACH_KV(k, v, c)
Definition: Foreach.h:197
auto v
std::map< int, std::string > bmMap
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
void setupBenchmark(size_t iters)
const char * string
Definition: Conv.cpp:212
KeyT k
BENCHMARK ( ForEachManual  ,
iters   
)

Definition at line 323 of file ForeachBenchmark.cpp.

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

323  {
324  int sum = 1;
325  for (size_t i = 1; i < iters; ++i) {
326  sum *= i;
327  }
328  doNotOptimizeAway(sum);
329 }
std::atomic< int64_t > sum(0)
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( ForEachRange  ,
iters   
)

Definition at line 331 of file ForeachBenchmark.cpp.

References folly::doNotOptimizeAway(), FOR_EACH_RANGE, i, and sum().

331  {
332  int sum = 1;
333  FOR_EACH_RANGE (i, 1, iters) { sum *= i; }
334  doNotOptimizeAway(sum);
335 }
std::atomic< int64_t > sum(0)
#define FOR_EACH_RANGE(i, begin, end)
Definition: Foreach.h:313
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( ForEachDescendingManual  ,
iters   
)

Definition at line 337 of file ForeachBenchmark.cpp.

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

337  {
338  int sum = 1;
339  for (size_t i = iters; i-- > 1;) {
340  sum *= i;
341  }
342  doNotOptimizeAway(sum);
343 }
std::atomic< int64_t > sum(0)
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( ForEachRangeR  ,
iters   
)

Definition at line 345 of file ForeachBenchmark.cpp.

References folly::doNotOptimizeAway(), FOR_EACH_RANGE_R, i, and sum().

345  {
346  int sum = 1;
347  FOR_EACH_RANGE_R (i, 1U, iters) { sum *= i; }
348  doNotOptimizeAway(sum);
349 }
std::atomic< int64_t > sum(0)
#define FOR_EACH_RANGE_R(i, begin, end)
Definition: Foreach.h:324
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( CharVecForRange  ,
iters   
)

Definition at line 351 of file ForeachBenchmark.cpp.

References BENCHMARK_SUSPEND, c, folly::doNotOptimizeAway(), setupCharVecBenchmark(), and sum().

351  {
353  setupCharVecBenchmark(iters);
354  }
355  size_t sum = 0;
356  for (auto& c : vec_char) {
357  sum += c;
358  }
359  doNotOptimizeAway(sum);
360 }
std::atomic< int64_t > sum(0)
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
void setupCharVecBenchmark(size_t iters)
std::vector< char > vec_char
char c
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( CharVecForRangeExplicitIndex  ,
iters   
)

Definition at line 362 of file ForeachBenchmark.cpp.

References BENCHMARK_SUSPEND, c, folly::doNotOptimizeAway(), setupCharVecBenchmark(), and sum().

362  {
364  setupCharVecBenchmark(iters);
365  }
366  size_t sum = 0;
367  size_t index = 0;
368  for (auto& c : vec_char) {
369  sum += c * index;
370  ++index;
371  }
372  doNotOptimizeAway(sum);
373 }
std::atomic< int64_t > sum(0)
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
void setupCharVecBenchmark(size_t iters)
std::vector< char > vec_char
char c
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( CharVecForEach  ,
iters   
)

Definition at line 375 of file ForeachBenchmark.cpp.

References BENCHMARK_SUSPEND, c, folly::doNotOptimizeAway(), folly::for_each(), setupCharVecBenchmark(), and sum().

375  {
377  setupCharVecBenchmark(iters);
378  }
379  size_t sum = 0;
380  folly::for_each(vec_char, [&](auto& c) { sum += c; });
381  doNotOptimizeAway(sum);
382 }
std::atomic< int64_t > sum(0)
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
void setupCharVecBenchmark(size_t iters)
std::vector< char > vec_char
char c
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( CharVecForEachIndex  ,
iters   
)

Definition at line 384 of file ForeachBenchmark.cpp.

References BENCHMARK_SUSPEND, c, folly::doNotOptimizeAway(), folly::for_each(), setupCharVecBenchmark(), and sum().

384  {
386  setupCharVecBenchmark(iters);
387  }
388  size_t sum = 0;
389  folly::for_each(vec_char, [&](auto& c, auto index) { sum += c * index; });
390  doNotOptimizeAway(sum);
391 }
std::atomic< int64_t > sum(0)
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
void setupCharVecBenchmark(size_t iters)
std::vector< char > vec_char
char c
FOLLY_CPP14_CONSTEXPR Func for_each(Sequence &&sequence, Func func)
Definition: Foreach-inl.h:314
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258
BENCHMARK ( CharVecForRangeEnumerate  ,
iters   
)

Definition at line 393 of file ForeachBenchmark.cpp.

References BENCHMARK_SUSPEND, folly::doNotOptimizeAway(), folly::enumerate(), setupCharVecBenchmark(), and sum().

393  {
395  setupCharVecBenchmark(iters);
396  }
397  size_t sum = 0;
398  for (auto&& it : enumerate(vec_char)) {
399  sum += *it * it.index;
400  }
401  doNotOptimizeAway(sum);
402 }
std::atomic< int64_t > sum(0)
#define BENCHMARK_SUSPEND
Definition: Benchmark.h:576
void setupCharVecBenchmark(size_t iters)
detail::RangeEnumerator< Range > enumerate(Range &&r)
Definition: Enumerate.h:167
std::vector< char > vec_char
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 404 of file ForeachBenchmark.cpp.

References folly::init(), and folly::runBenchmarks().

404  {
405  folly::init(&argc, &argv);
406  runBenchmarks();
407  return 0;
408 }
void runBenchmarks()
Definition: Benchmark.cpp:456
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
char ** argv
void setupBenchmark ( size_t  iters)

Definition at line 44 of file ForeachBenchmark.cpp.

References i.

Referenced by BENCHMARK().

44  {
45  bmMap.clear();
46  for (size_t i = 0; i < iters; ++i) {
47  bmMap[i] = "teststring";
48  }
49 
50  vec_one.clear();
51  vec_two.clear();
52  vec_one.resize(iters);
53  vec_two.resize(iters);
54 }
std::map< int, std::string > bmMap
std::vector< int > vec_one
std::vector< int > vec_two
void setupCharVecBenchmark ( size_t  iters)

Definition at line 56 of file ForeachBenchmark.cpp.

References folly::Random::rand32().

Referenced by BENCHMARK().

56  {
57  vec_char.resize(iters);
58  std::generate(
59  vec_char.begin(), vec_char.end(), [] { return Random::rand32(128); });
60 }
std::vector< char > vec_char

Variable Documentation

std::map<int, std::string> bmMap

Definition at line 37 of file ForeachBenchmark.cpp.

std::vector<char> vec_char

Definition at line 42 of file ForeachBenchmark.cpp.

std::vector<int> vec_one

Definition at line 38 of file ForeachBenchmark.cpp.

std::vector<int> vec_two

Definition at line 39 of file ForeachBenchmark.cpp.