|
| 1 | +#include "poly2variant/poly2variant.hpp" |
| 2 | +#include "poly_visitor/visitor.hpp" |
| 3 | +#include "timer.hpp" |
| 4 | +#include <boost/mpl/for_each.hpp> |
| 5 | +#include <boost/variant.hpp> |
| 6 | +#include <boost/fusion/tuple.hpp> |
| 7 | +#include <boost/fusion/adapted.hpp> |
| 8 | +#include <boost/fusion/algorithm.hpp> |
| 9 | +#include <iostream> |
| 10 | +#include <string> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +struct D1; |
| 14 | +struct D2; |
| 15 | +struct D3; |
| 16 | +struct D4; |
| 17 | +struct D5; |
| 18 | +struct D6; |
| 19 | +struct D7; |
| 20 | + |
| 21 | +using base_visitor = poly_visitor::base_visitor |
| 22 | + <D1, D2, D3, D4, D5, D6, D7>; |
| 23 | + |
| 24 | +struct B |
| 25 | +{ PURE_VISITABLE(base_visitor) }; |
| 26 | +struct D1 : B |
| 27 | +{ |
| 28 | + VISITABLE(base_visitor) |
| 29 | + virtual ~D1() = default; |
| 30 | +}; |
| 31 | +struct D2 : B |
| 32 | +{ |
| 33 | + VISITABLE(base_visitor) |
| 34 | + virtual ~D2() = default; |
| 35 | +}; |
| 36 | +struct D3 : B |
| 37 | +{ |
| 38 | + VISITABLE(base_visitor) |
| 39 | + virtual ~D3() = default; |
| 40 | +}; |
| 41 | +struct D4 : B |
| 42 | +{ |
| 43 | + VISITABLE(base_visitor) |
| 44 | + virtual ~D4() = default; |
| 45 | +}; |
| 46 | +struct D5 : B |
| 47 | +{ |
| 48 | + VISITABLE(base_visitor) |
| 49 | + virtual ~D5() = default; |
| 50 | +}; |
| 51 | +struct D6 : B |
| 52 | +{ |
| 53 | + VISITABLE(base_visitor) |
| 54 | + virtual ~D6() = default; |
| 55 | +}; |
| 56 | +struct D7 : B |
| 57 | +{ |
| 58 | + VISITABLE(base_visitor) |
| 59 | + virtual ~D7() = default; |
| 60 | +}; |
| 61 | + |
| 62 | +struct Visitor |
| 63 | +{ |
| 64 | + using result_type = void; |
| 65 | + result_type operator()(const D1&) const |
| 66 | + {} |
| 67 | + result_type operator()(const D2&) const |
| 68 | + {} |
| 69 | + result_type operator()(const D3&) const |
| 70 | + {} |
| 71 | + result_type operator()(const D4&) const |
| 72 | + {} |
| 73 | + result_type operator()(const D5&) const |
| 74 | + {} |
| 75 | + result_type operator()(const D6&) const |
| 76 | + {} |
| 77 | + result_type operator()(const D7&) const |
| 78 | + {} |
| 79 | +}; |
| 80 | + |
| 81 | +using visitables = boost::mpl::vector<D1, D2, D3, D4, D5, D6, D7>; |
| 82 | + |
| 83 | +struct PtrVisitor |
| 84 | +{ |
| 85 | + using result_type = void; |
| 86 | + using visitables = ::visitables; |
| 87 | + result_type operator()(const D1*) const |
| 88 | + {} |
| 89 | + result_type operator()(const D2*) const |
| 90 | + {} |
| 91 | + result_type operator()(const D3*) const |
| 92 | + {} |
| 93 | + result_type operator()(const D4*) const |
| 94 | + {} |
| 95 | + result_type operator()(const D5*) const |
| 96 | + {} |
| 97 | + result_type operator()(const D6*) const |
| 98 | + {} |
| 99 | + result_type operator()(const D7*) const |
| 100 | + {} |
| 101 | +}; |
| 102 | + |
| 103 | +struct FillVector |
| 104 | +{ |
| 105 | + FillVector(std::vector<std::unique_ptr<B>>& vec) : vec(vec) {} |
| 106 | + template<typename T> |
| 107 | + void operator()(T) |
| 108 | + { vec.emplace_back(new typename std::remove_pointer<T>::type); } |
| 109 | + std::vector<std::unique_ptr<B>>& vec; |
| 110 | +}; |
| 111 | + |
| 112 | +int main(int argc, char **argv) |
| 113 | +{ |
| 114 | + std::size_t n(1); |
| 115 | + |
| 116 | + if(argc > 1) |
| 117 | + n = std::stol(argv[1]); |
| 118 | + |
| 119 | + std::cout << "usage: benchmark [number_of_iterations]" |
| 120 | + << std::endl << std::endl |
| 121 | + << "[Number of iterations] = " << n |
| 122 | + << std::endl; |
| 123 | + |
| 124 | + std::vector<std::unique_ptr<B>> vec; |
| 125 | + for(std::size_t i(0); i < n; ++i) |
| 126 | + boost::mpl::for_each<visitables>(FillVector(vec)); |
| 127 | + |
| 128 | + { |
| 129 | + timer<std::chrono::microseconds> t("poly visitor "); |
| 130 | + for(std::size_t i(0); i < n; ++i) |
| 131 | + { |
| 132 | + auto base = vec[i].get(); |
| 133 | + poly_visitor::apply_visitor(Visitor{}, *base); |
| 134 | + } |
| 135 | + } |
| 136 | + { |
| 137 | + timer<std::chrono::microseconds> t("boost variant"); |
| 138 | + for(std::size_t i(0); i < n; ++i) |
| 139 | + { |
| 140 | + auto base = vec[i].get(); |
| 141 | + poly2variant::apply_visitor(PtrVisitor{}, base); |
| 142 | + } |
| 143 | + } |
| 144 | + { |
| 145 | + timer<std::chrono::microseconds> t("dynamic_cast "); |
| 146 | + for(std::size_t i(0); i < n; ++i) |
| 147 | + { |
| 148 | + auto base = vec[i].get(); |
| 149 | + if(dynamic_cast<D1*>(base)) |
| 150 | + {} |
| 151 | + else if(dynamic_cast<D2*>(base)) |
| 152 | + {} |
| 153 | + else if(dynamic_cast<D3*>(base)) |
| 154 | + {} |
| 155 | + else if(dynamic_cast<D4*>(base)) |
| 156 | + {} |
| 157 | + else if(dynamic_cast<D5*>(base)) |
| 158 | + {} |
| 159 | + else if(dynamic_cast<D6*>(base)) |
| 160 | + {} |
| 161 | + else if(dynamic_cast<D7*>(base)) |
| 162 | + {} |
| 163 | + else |
| 164 | + { |
| 165 | + //Turn off optimizations. This branch forces the |
| 166 | + //compiler to consider the type switching at runtime. |
| 167 | + abort(); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments