benchmarks.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Benchmarks for absl random distributions as well as a selection of the
  15. // C++ standard library random distributions.
  16. #include <algorithm>
  17. #include <cstddef>
  18. #include <cstdint>
  19. #include <initializer_list>
  20. #include <iterator>
  21. #include <limits>
  22. #include <random>
  23. #include <type_traits>
  24. #include <vector>
  25. #include "absl/base/macros.h"
  26. #include "absl/meta/type_traits.h"
  27. #include "absl/random/bernoulli_distribution.h"
  28. #include "absl/random/beta_distribution.h"
  29. #include "absl/random/exponential_distribution.h"
  30. #include "absl/random/gaussian_distribution.h"
  31. #include "absl/random/internal/fast_uniform_bits.h"
  32. #include "absl/random/internal/randen_engine.h"
  33. #include "absl/random/log_uniform_int_distribution.h"
  34. #include "absl/random/poisson_distribution.h"
  35. #include "absl/random/random.h"
  36. #include "absl/random/uniform_int_distribution.h"
  37. #include "absl/random/uniform_real_distribution.h"
  38. #include "absl/random/zipf_distribution.h"
  39. #include "benchmark/benchmark.h"
  40. namespace {
  41. // Seed data to avoid reading random_device() for benchmarks.
  42. uint32_t kSeedData[] = {
  43. 0x1B510052, 0x9A532915, 0xD60F573F, 0xBC9BC6E4, 0x2B60A476, 0x81E67400,
  44. 0x08BA6FB5, 0x571BE91F, 0xF296EC6B, 0x2A0DD915, 0xB6636521, 0xE7B9F9B6,
  45. 0xFF34052E, 0xC5855664, 0x53B02D5D, 0xA99F8FA1, 0x08BA4799, 0x6E85076A,
  46. 0x4B7A70E9, 0xB5B32944, 0xDB75092E, 0xC4192623, 0xAD6EA6B0, 0x49A7DF7D,
  47. 0x9CEE60B8, 0x8FEDB266, 0xECAA8C71, 0x699A18FF, 0x5664526C, 0xC2B19EE1,
  48. 0x193602A5, 0x75094C29, 0xA0591340, 0xE4183A3E, 0x3F54989A, 0x5B429D65,
  49. 0x6B8FE4D6, 0x99F73FD6, 0xA1D29C07, 0xEFE830F5, 0x4D2D38E6, 0xF0255DC1,
  50. 0x4CDD2086, 0x8470EB26, 0x6382E9C6, 0x021ECC5E, 0x09686B3F, 0x3EBAEFC9,
  51. 0x3C971814, 0x6B6A70A1, 0x687F3584, 0x52A0E286, 0x13198A2E, 0x03707344,
  52. };
  53. // PrecompiledSeedSeq provides kSeedData to a conforming
  54. // random engine to speed initialization in the benchmarks.
  55. class PrecompiledSeedSeq {
  56. public:
  57. using result_type = uint32_t;
  58. PrecompiledSeedSeq() {}
  59. template <typename Iterator>
  60. PrecompiledSeedSeq(Iterator begin, Iterator end) {}
  61. template <typename T>
  62. PrecompiledSeedSeq(std::initializer_list<T> il) {}
  63. template <typename OutIterator>
  64. void generate(OutIterator begin, OutIterator end) {
  65. static size_t idx = 0;
  66. for (; begin != end; begin++) {
  67. *begin = kSeedData[idx++];
  68. if (idx >= ABSL_ARRAYSIZE(kSeedData)) {
  69. idx = 0;
  70. }
  71. }
  72. }
  73. size_t size() const { return ABSL_ARRAYSIZE(kSeedData); }
  74. template <typename OutIterator>
  75. void param(OutIterator out) const {
  76. std::copy(std::begin(kSeedData), std::end(kSeedData), out);
  77. }
  78. };
  79. // use_default_initialization<T> indicates whether the random engine
  80. // T must be default initialized, or whether we may initialize it using
  81. // a seed sequence. This is used because some engines do not accept seed
  82. // sequence-based initialization.
  83. template <typename E>
  84. using use_default_initialization = std::false_type;
  85. // make_engine<T, SSeq> returns a random_engine which is initialized,
  86. // either via the default constructor, when use_default_initialization<T>
  87. // is true, or via the indicated seed sequence, SSeq.
  88. template <typename Engine, typename SSeq = PrecompiledSeedSeq>
  89. typename absl::enable_if_t<!use_default_initialization<Engine>::value, Engine>
  90. make_engine() {
  91. // Initialize the random engine using the seed sequence SSeq, which
  92. // is constructed from the precompiled seed data.
  93. SSeq seq(std::begin(kSeedData), std::end(kSeedData));
  94. return Engine(seq);
  95. }
  96. template <typename Engine, typename SSeq = PrecompiledSeedSeq>
  97. typename absl::enable_if_t<use_default_initialization<Engine>::value, Engine>
  98. make_engine() {
  99. // Initialize the random engine using the default constructor.
  100. return Engine();
  101. }
  102. template <typename Engine, typename SSeq>
  103. void BM_Construct(benchmark::State& state) {
  104. for (auto _ : state) {
  105. auto rng = make_engine<Engine, SSeq>();
  106. benchmark::DoNotOptimize(rng());
  107. }
  108. }
  109. template <typename Engine>
  110. void BM_Direct(benchmark::State& state) {
  111. using value_type = typename Engine::result_type;
  112. // Direct use of the URBG.
  113. auto rng = make_engine<Engine>();
  114. for (auto _ : state) {
  115. benchmark::DoNotOptimize(rng());
  116. }
  117. state.SetBytesProcessed(sizeof(value_type) * state.iterations());
  118. }
  119. template <typename Engine>
  120. void BM_Generate(benchmark::State& state) {
  121. // std::generate makes a copy of the RNG; thus this tests the
  122. // copy-constructor efficiency.
  123. using value_type = typename Engine::result_type;
  124. std::vector<value_type> v(64);
  125. auto rng = make_engine<Engine>();
  126. while (state.KeepRunningBatch(64)) {
  127. std::generate(std::begin(v), std::end(v), rng);
  128. }
  129. }
  130. template <typename Engine, size_t elems>
  131. void BM_Shuffle(benchmark::State& state) {
  132. // Direct use of the Engine.
  133. std::vector<uint32_t> v(elems);
  134. while (state.KeepRunningBatch(elems)) {
  135. auto rng = make_engine<Engine>();
  136. std::shuffle(std::begin(v), std::end(v), rng);
  137. }
  138. }
  139. template <typename Engine, size_t elems>
  140. void BM_ShuffleReuse(benchmark::State& state) {
  141. // Direct use of the Engine.
  142. std::vector<uint32_t> v(elems);
  143. auto rng = make_engine<Engine>();
  144. while (state.KeepRunningBatch(elems)) {
  145. std::shuffle(std::begin(v), std::end(v), rng);
  146. }
  147. }
  148. template <typename Engine, typename Dist, typename... Args>
  149. void BM_Dist(benchmark::State& state, Args&&... args) {
  150. using value_type = typename Dist::result_type;
  151. auto rng = make_engine<Engine>();
  152. Dist dis{std::forward<Args>(args)...};
  153. // Compare the following loop performance:
  154. for (auto _ : state) {
  155. benchmark::DoNotOptimize(dis(rng));
  156. }
  157. state.SetBytesProcessed(sizeof(value_type) * state.iterations());
  158. }
  159. template <typename Engine, typename Dist>
  160. void BM_Large(benchmark::State& state) {
  161. using value_type = typename Dist::result_type;
  162. volatile value_type kMin = 0;
  163. volatile value_type kMax = std::numeric_limits<value_type>::max() / 2 + 1;
  164. BM_Dist<Engine, Dist>(state, kMin, kMax);
  165. }
  166. template <typename Engine, typename Dist>
  167. void BM_Small(benchmark::State& state) {
  168. using value_type = typename Dist::result_type;
  169. volatile value_type kMin = 0;
  170. volatile value_type kMax = std::numeric_limits<value_type>::max() / 64 + 1;
  171. BM_Dist<Engine, Dist>(state, kMin, kMax);
  172. }
  173. template <typename Engine, typename Dist, int A>
  174. void BM_Bernoulli(benchmark::State& state) {
  175. volatile double a = static_cast<double>(A) / 1000000;
  176. BM_Dist<Engine, Dist>(state, a);
  177. }
  178. template <typename Engine, typename Dist, int A, int B>
  179. void BM_Beta(benchmark::State& state) {
  180. using value_type = typename Dist::result_type;
  181. volatile value_type a = static_cast<value_type>(A) / 100;
  182. volatile value_type b = static_cast<value_type>(B) / 100;
  183. BM_Dist<Engine, Dist>(state, a, b);
  184. }
  185. template <typename Engine, typename Dist, int A>
  186. void BM_Gamma(benchmark::State& state) {
  187. using value_type = typename Dist::result_type;
  188. volatile value_type a = static_cast<value_type>(A) / 100;
  189. BM_Dist<Engine, Dist>(state, a);
  190. }
  191. template <typename Engine, typename Dist, int A = 100>
  192. void BM_Poisson(benchmark::State& state) {
  193. volatile double a = static_cast<double>(A) / 100;
  194. BM_Dist<Engine, Dist>(state, a);
  195. }
  196. template <typename Engine, typename Dist, int Q = 2, int V = 1>
  197. void BM_Zipf(benchmark::State& state) {
  198. using value_type = typename Dist::result_type;
  199. volatile double q = Q;
  200. volatile double v = V;
  201. BM_Dist<Engine, Dist>(state, std::numeric_limits<value_type>::max(), q, v);
  202. }
  203. template <typename Engine, typename Dist>
  204. void BM_Thread(benchmark::State& state) {
  205. using value_type = typename Dist::result_type;
  206. auto rng = make_engine<Engine>();
  207. Dist dis{};
  208. for (auto _ : state) {
  209. benchmark::DoNotOptimize(dis(rng));
  210. }
  211. state.SetBytesProcessed(sizeof(value_type) * state.iterations());
  212. }
  213. // NOTES:
  214. //
  215. // std::geometric_distribution is similar to the zipf distributions.
  216. // The algorithm for the geometric_distribution is, basically,
  217. // floor(log(1-X) / log(1-p))
  218. // Normal benchmark suite
  219. #define BM_BASIC(Engine) \
  220. BENCHMARK_TEMPLATE(BM_Construct, Engine, PrecompiledSeedSeq); \
  221. BENCHMARK_TEMPLATE(BM_Construct, Engine, std::seed_seq); \
  222. BENCHMARK_TEMPLATE(BM_Direct, Engine); \
  223. BENCHMARK_TEMPLATE(BM_Shuffle, Engine, 10); \
  224. BENCHMARK_TEMPLATE(BM_Shuffle, Engine, 100); \
  225. BENCHMARK_TEMPLATE(BM_Shuffle, Engine, 1000); \
  226. BENCHMARK_TEMPLATE(BM_ShuffleReuse, Engine, 100); \
  227. BENCHMARK_TEMPLATE(BM_ShuffleReuse, Engine, 1000); \
  228. BENCHMARK_TEMPLATE(BM_Dist, Engine, \
  229. absl::random_internal::FastUniformBits<uint32_t>); \
  230. BENCHMARK_TEMPLATE(BM_Dist, Engine, \
  231. absl::random_internal::FastUniformBits<uint64_t>); \
  232. BENCHMARK_TEMPLATE(BM_Dist, Engine, std::uniform_int_distribution<int32_t>); \
  233. BENCHMARK_TEMPLATE(BM_Dist, Engine, std::uniform_int_distribution<int64_t>); \
  234. BENCHMARK_TEMPLATE(BM_Dist, Engine, \
  235. absl::uniform_int_distribution<int32_t>); \
  236. BENCHMARK_TEMPLATE(BM_Dist, Engine, \
  237. absl::uniform_int_distribution<int64_t>); \
  238. BENCHMARK_TEMPLATE(BM_Large, Engine, \
  239. std::uniform_int_distribution<int32_t>); \
  240. BENCHMARK_TEMPLATE(BM_Large, Engine, \
  241. std::uniform_int_distribution<int64_t>); \
  242. BENCHMARK_TEMPLATE(BM_Large, Engine, \
  243. absl::uniform_int_distribution<int32_t>); \
  244. BENCHMARK_TEMPLATE(BM_Large, Engine, \
  245. absl::uniform_int_distribution<int64_t>); \
  246. BENCHMARK_TEMPLATE(BM_Dist, Engine, std::uniform_real_distribution<float>); \
  247. BENCHMARK_TEMPLATE(BM_Dist, Engine, std::uniform_real_distribution<double>); \
  248. BENCHMARK_TEMPLATE(BM_Dist, Engine, absl::uniform_real_distribution<float>); \
  249. BENCHMARK_TEMPLATE(BM_Dist, Engine, absl::uniform_real_distribution<double>)
  250. #define BM_COPY(Engine) BENCHMARK_TEMPLATE(BM_Generate, Engine)
  251. #define BM_THREAD(Engine) \
  252. BENCHMARK_TEMPLATE(BM_Thread, Engine, \
  253. absl::uniform_int_distribution<int64_t>) \
  254. ->ThreadPerCpu(); \
  255. BENCHMARK_TEMPLATE(BM_Thread, Engine, \
  256. absl::uniform_real_distribution<double>) \
  257. ->ThreadPerCpu(); \
  258. BENCHMARK_TEMPLATE(BM_Shuffle, Engine, 100)->ThreadPerCpu(); \
  259. BENCHMARK_TEMPLATE(BM_Shuffle, Engine, 1000)->ThreadPerCpu(); \
  260. BENCHMARK_TEMPLATE(BM_ShuffleReuse, Engine, 100)->ThreadPerCpu(); \
  261. BENCHMARK_TEMPLATE(BM_ShuffleReuse, Engine, 1000)->ThreadPerCpu();
  262. #define BM_EXTENDED(Engine) \
  263. /* -------------- Extended Uniform -----------------------*/ \
  264. BENCHMARK_TEMPLATE(BM_Small, Engine, \
  265. std::uniform_int_distribution<int32_t>); \
  266. BENCHMARK_TEMPLATE(BM_Small, Engine, \
  267. std::uniform_int_distribution<int64_t>); \
  268. BENCHMARK_TEMPLATE(BM_Small, Engine, \
  269. absl::uniform_int_distribution<int32_t>); \
  270. BENCHMARK_TEMPLATE(BM_Small, Engine, \
  271. absl::uniform_int_distribution<int64_t>); \
  272. BENCHMARK_TEMPLATE(BM_Small, Engine, std::uniform_real_distribution<float>); \
  273. BENCHMARK_TEMPLATE(BM_Small, Engine, \
  274. std::uniform_real_distribution<double>); \
  275. BENCHMARK_TEMPLATE(BM_Small, Engine, \
  276. absl::uniform_real_distribution<float>); \
  277. BENCHMARK_TEMPLATE(BM_Small, Engine, \
  278. absl::uniform_real_distribution<double>); \
  279. /* -------------- Other -----------------------*/ \
  280. BENCHMARK_TEMPLATE(BM_Dist, Engine, std::normal_distribution<double>); \
  281. BENCHMARK_TEMPLATE(BM_Dist, Engine, absl::gaussian_distribution<double>); \
  282. BENCHMARK_TEMPLATE(BM_Dist, Engine, std::exponential_distribution<double>); \
  283. BENCHMARK_TEMPLATE(BM_Dist, Engine, absl::exponential_distribution<double>); \
  284. BENCHMARK_TEMPLATE(BM_Poisson, Engine, std::poisson_distribution<int64_t>, \
  285. 100); \
  286. BENCHMARK_TEMPLATE(BM_Poisson, Engine, absl::poisson_distribution<int64_t>, \
  287. 100); \
  288. BENCHMARK_TEMPLATE(BM_Poisson, Engine, std::poisson_distribution<int64_t>, \
  289. 10 * 100); \
  290. BENCHMARK_TEMPLATE(BM_Poisson, Engine, absl::poisson_distribution<int64_t>, \
  291. 10 * 100); \
  292. BENCHMARK_TEMPLATE(BM_Poisson, Engine, std::poisson_distribution<int64_t>, \
  293. 13 * 100); \
  294. BENCHMARK_TEMPLATE(BM_Poisson, Engine, absl::poisson_distribution<int64_t>, \
  295. 13 * 100); \
  296. BENCHMARK_TEMPLATE(BM_Dist, Engine, \
  297. absl::log_uniform_int_distribution<int32_t>); \
  298. BENCHMARK_TEMPLATE(BM_Dist, Engine, \
  299. absl::log_uniform_int_distribution<int64_t>); \
  300. BENCHMARK_TEMPLATE(BM_Dist, Engine, std::geometric_distribution<int64_t>); \
  301. BENCHMARK_TEMPLATE(BM_Zipf, Engine, absl::zipf_distribution<uint64_t>); \
  302. BENCHMARK_TEMPLATE(BM_Zipf, Engine, absl::zipf_distribution<uint64_t>, 2, \
  303. 3); \
  304. BENCHMARK_TEMPLATE(BM_Bernoulli, Engine, std::bernoulli_distribution, \
  305. 257305); \
  306. BENCHMARK_TEMPLATE(BM_Bernoulli, Engine, absl::bernoulli_distribution, \
  307. 257305); \
  308. BENCHMARK_TEMPLATE(BM_Beta, Engine, absl::beta_distribution<double>, 65, \
  309. 41); \
  310. BENCHMARK_TEMPLATE(BM_Beta, Engine, absl::beta_distribution<double>, 99, \
  311. 330); \
  312. BENCHMARK_TEMPLATE(BM_Beta, Engine, absl::beta_distribution<double>, 150, \
  313. 150); \
  314. BENCHMARK_TEMPLATE(BM_Beta, Engine, absl::beta_distribution<double>, 410, \
  315. 580); \
  316. BENCHMARK_TEMPLATE(BM_Beta, Engine, absl::beta_distribution<float>, 65, 41); \
  317. BENCHMARK_TEMPLATE(BM_Beta, Engine, absl::beta_distribution<float>, 99, \
  318. 330); \
  319. BENCHMARK_TEMPLATE(BM_Beta, Engine, absl::beta_distribution<float>, 150, \
  320. 150); \
  321. BENCHMARK_TEMPLATE(BM_Beta, Engine, absl::beta_distribution<float>, 410, \
  322. 580); \
  323. BENCHMARK_TEMPLATE(BM_Gamma, Engine, std::gamma_distribution<float>, 199); \
  324. BENCHMARK_TEMPLATE(BM_Gamma, Engine, std::gamma_distribution<double>, 199);
  325. // ABSL Recommended interfaces.
  326. BM_BASIC(absl::InsecureBitGen); // === pcg64_2018_engine
  327. BM_BASIC(absl::BitGen); // === randen_engine<uint64_t>.
  328. BM_THREAD(absl::BitGen);
  329. BM_EXTENDED(absl::BitGen);
  330. // Instantiate benchmarks for multiple engines.
  331. using randen_engine_64 = absl::random_internal::randen_engine<uint64_t>;
  332. using randen_engine_32 = absl::random_internal::randen_engine<uint32_t>;
  333. // Comparison interfaces.
  334. BM_BASIC(std::mt19937_64);
  335. BM_COPY(std::mt19937_64);
  336. BM_EXTENDED(std::mt19937_64);
  337. BM_BASIC(randen_engine_64);
  338. BM_COPY(randen_engine_64);
  339. BM_EXTENDED(randen_engine_64);
  340. BM_BASIC(std::mt19937);
  341. BM_COPY(std::mt19937);
  342. BM_BASIC(randen_engine_32);
  343. BM_COPY(randen_engine_32);
  344. } // namespace