1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright 2012 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #pragma warning(disable:4244)
10 #include <boost/polygon/detail/voronoi_predicates.hpp>
11 #include <boost/polygon/detail/voronoi_structures.hpp>
12 #include <boost/polygon/detail/skeleton_predicates.hpp>
13 #include <boost/random/mersenne_twister.hpp>
14 #include <boost/random/uniform_int_distribution.hpp>
17 #include <boost/chrono.hpp>
18 #include <boost/multiprecision/cpp_int.hpp>
21 #include <boost/multiprecision/gmp.hpp>
24 #include <boost/multiprecision/tommath.hpp>
27 #include "arithmetic_backend.hpp"
29 typedef boost::polygon::detail::point_2d<boost::int32_t> i_point;
31 template <class Clock>
34 typedef typename Clock::duration duration;
37 m_start = Clock::now();
41 return Clock::now() - m_start;
45 m_start = Clock::now();
49 typename Clock::time_point m_start;
52 std::vector<i_point> points;
53 boost::random::mt19937 gen;
56 struct cpp_int_voronoi_traits
58 typedef boost::int32_t int_type;
59 typedef boost::int64_t int_x2_type;
60 typedef boost::uint64_t uint_x2_type;
61 typedef Big big_int_type;
62 typedef double fpt_type;
63 typedef boost::polygon::detail::extended_exponent_fpt<fpt_type> efpt_type;
64 typedef boost::polygon::detail::ulp_comparison<fpt_type> ulp_cmp_type;
65 struct to_fpt_converter_type
67 template <class B, boost::multiprecision::expression_template_option ET>
68 double operator ()(const boost::multiprecision::number<B, ET>& val)
70 return val.template convert_to<double>();
72 double operator ()(double val)
76 double operator()(const efpt_type& that) const
80 template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
81 double operator()(const boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
83 typedef typename boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type r_t;
85 return r.template convert_to<double>();
88 struct to_efpt_converter_type
90 template <class B, boost::multiprecision::expression_template_option ET>
91 efpt_type operator ()(const boost::multiprecision::number<B, ET>& val)
93 return efpt_type(val.template convert_to<double>(), 0);
95 efpt_type operator ()(double val)
97 return efpt_type(val, 0);
99 template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
100 double operator()(const boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
102 typedef typename boost::multiprecision::detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type r_t;
104 return efpt_type(r.template convert_to<double>(), 0);
110 struct native_int_voronoi_traits
112 typedef boost::int32_t int_type;
113 typedef boost::int64_t int_x2_type;
114 typedef boost::uint64_t uint_x2_type;
115 typedef Big big_int_type;
116 typedef double fpt_type;
117 typedef boost::polygon::detail::extended_exponent_fpt<fpt_type> efpt_type;
118 typedef boost::polygon::detail::ulp_comparison<fpt_type> ulp_cmp_type;
119 struct to_fpt_converter_type
122 double operator ()(const T& val)const
126 double operator()(const efpt_type& that) const
131 struct to_efpt_converter_type
134 efpt_type operator ()(const T& val)const
136 return efpt_type(val, 0);
141 std::map<std::string, double> results;
142 double min_time = (std::numeric_limits<double>::max)();
144 template <class Traits>
145 double test(const char* name)
147 typedef boost::polygon::detail::voronoi_predicates<Traits> preds;
148 typedef boost::polygon::detail::circle_event<boost::int32_t> circle_event;
149 typedef boost::polygon::detail::site_event<boost::int32_t> site_event;
150 typedef typename preds::template mp_circle_formation_functor<site_event, circle_event> circle_pred;
152 boost::random::uniform_int_distribution<> dist(0, points.size() - 1);
156 stopwatch<boost::chrono::high_resolution_clock> w;
158 for(unsigned i = 0; i < 10000; ++i)
160 site_event s1(points[dist(gen)]);
161 site_event s2(points[dist(gen)]);
162 site_event s3(points[dist(gen)]);
163 pc.ppp(s1, s2, s3, event);
164 pc.pps(s1, s2, s3, 0, event);
165 pc.pss(s1, s2, s3, 0, event);
166 pc.sss(s1, s2, s3, event);
168 double d = boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
172 std::cout << "Time for " << std::setw(30) << std::left << name << " = " << d << std::endl;
176 void generate_quickbook()
178 std::cout << "[table\n[[Integer Type][Relative Performance (Actual time in parenthesis)]]\n";
180 std::map<std::string, double>::const_iterator i(results.begin()), j(results.end());
184 double rel = i->second / min_time;
185 std::cout << "[[" << i->first << "][" << rel << "(" << i->second << "s)]]\n";
195 boost::random::uniform_int_distribution<> dist((std::numeric_limits<boost::int32_t>::min)() / 2, (std::numeric_limits<boost::int32_t>::max)() / 2);
197 for(unsigned i = 0; i < 100; ++i)
199 points.push_back(i_point(dist(gen), dist(gen)));
202 test<boost::polygon::detail::voronoi_ctype_traits<boost::int32_t> >("extended_int");
204 test<cpp_int_voronoi_traits<boost::multiprecision::int256_t> >("int256_t");
205 test<cpp_int_voronoi_traits<boost::multiprecision::int512_t> >("int512_t");
206 test<cpp_int_voronoi_traits<boost::multiprecision::int1024_t> >("int1024_t");
208 test<cpp_int_voronoi_traits<boost::multiprecision::checked_int256_t> >("checked_int256_t");
209 test<cpp_int_voronoi_traits<boost::multiprecision::checked_int512_t> >("checked_int512_t");
210 test<cpp_int_voronoi_traits<boost::multiprecision::checked_int1024_t> >("checked_int1024_t");
212 test<cpp_int_voronoi_traits<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<>, boost::multiprecision::et_off> > >("cpp_int");
215 test<cpp_int_voronoi_traits<boost::multiprecision::number<boost::multiprecision::gmp_int, boost::multiprecision::et_off> > >("mpz_int");
218 test<cpp_int_voronoi_traits<boost::multiprecision::number<boost::multiprecision::tommath_int, boost::multiprecision::et_off> > >("tom_int");
221 generate_quickbook();
223 test<native_int_voronoi_traits<boost::int64_t> >("int64_t");
224 test<cpp_int_voronoi_traits<boost::multiprecision::number<boost::multiprecision::arithmetic_backend<boost::int64_t>, boost::multiprecision::et_off> > >("number<arithmetic_backend<boost::int64_t>, et_off>");
225 //test<cpp_int_voronoi_traits<boost::multiprecision::number<boost::multiprecision::arithmetic_backend<boost::int64_t>, boost::multiprecision::et_on> > >("number<arithmetic_backend<boost::int64_t>, et_on>");