PIVX Core  5.6.99
P2P Digital Currency
prevector.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2017 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "bench/bench.h"
6 
7 #include "compat.h"
8 #include "prevector.h"
9 #include "serialize.h"
10 #include "streams.h"
11 
12 #include <type_traits>
13 
14 // GCC 4.8 is missing some C++11 type_traits,
15 // https://www.gnu.org/software/gcc/gcc-5/changes.html
16 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
17 #define IS_TRIVIALLY_CONSTRUCTIBLE std::has_trivial_default_constructor
18 #else
19 #define IS_TRIVIALLY_CONSTRUCTIBLE std::is_trivially_default_constructible
20 #endif
21 
22 struct nontrivial_t {
23  int x;
24  nontrivial_t() :x(-1) {}
26 };
27 static_assert(!IS_TRIVIALLY_CONSTRUCTIBLE<nontrivial_t>::value,
28  "expected nontrivial_t to not be trivially constructible");
29 
30 typedef unsigned char trivial_t;
31 static_assert(IS_TRIVIALLY_CONSTRUCTIBLE<trivial_t>::value,
32  "expected trivial_t to be trivially constructible");
33 
34 template <typename T>
35 static void PrevectorDestructor(benchmark::State& state)
36 {
37  while (state.KeepRunning()) {
38  for (auto x = 0; x < 1000; ++x) {
41  t0.resize(28);
42  t1.resize(29);
43  }
44  }
45 }
46 
47 template <typename T>
48 static void PrevectorClear(benchmark::State& state)
49 {
50 
51  while (state.KeepRunning()) {
52  for (auto x = 0; x < 1000; ++x) {
55  t0.resize(28);
56  t0.clear();
57  t1.resize(29);
58  t0.clear();
59  }
60  }
61 }
62 
63 template <typename T>
65 {
66  while (state.KeepRunning()) {
69  for (auto x = 0; x < 1000; ++x) {
70  t0.resize(28);
71  t0.resize(0);
72  t1.resize(29);
73  t1.resize(0);
74  }
75  }
76 }
77 
78 #define PREVECTOR_TEST(name, nontrivops, trivops) \
79  static void Prevector ## name ## Nontrivial(benchmark::State& state) { \
80  PrevectorResize<nontrivial_t>(state); \
81  } \
82  BENCHMARK(Prevector ## name ## Nontrivial, nontrivops); \
83  static void Prevector ## name ## Trivial(benchmark::State& state) { \
84  PrevectorResize<trivial_t>(state); \
85  } \
86  BENCHMARK(Prevector ## name ## Trivial, trivops);
87 
88 PREVECTOR_TEST(Clear, 28300, 88600)
89 PREVECTOR_TEST(Destructor, 28800, 88900)
90 PREVECTOR_TEST(Resize, 28900, 90300)
bool KeepRunning()
Definition: bench.h:69
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
void clear()
Definition: prevector.h:338
void resize(size_type new_size)
Definition: prevector.h:311
#define PREVECTOR_TEST(name, nontrivops, trivops)
Definition: prevector.cpp:78
unsigned char trivial_t
Definition: prevector.cpp:28
void PrevectorResize(benchmark::State &state)
Definition: prevector.cpp:64
#define READWRITE(...)
Definition: serialize.h:183
SERIALIZE_METHODS(nontrivial_t, obj)
Definition: prevector.cpp:25