1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
///////////////////////////////////////////////////////////////
// Copyright 2017 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
#define BOOST_CHRONO_HEADER_ONLY
#ifdef _MSC_VER
# define _SCL_SECURE_NO_WARNINGS
#endif
#include "performance.hpp"
#include "table_helper.hpp"
#include <boost/random.hpp>
#include <boost/math/tools/polynomial.hpp>
#include <boost/multiprecision/cpp_int.hpp>
unsigned max_reps = 1000;
template <class T>
struct tester
{
tester()
{
a.assign(500, T());
for(int i = 0; i < 500; ++i)
{
b.push_back(generate_random(false));
c.push_back(generate_random(false));
small.push_back(generate_random(true));
}
}
double test_add()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for(unsigned i = 0; i < b.size(); ++i)
a[i] = b[i] + c[i];
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_subtract()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for(unsigned i = 0; i < b.size(); ++i)
a[i] = b[i] - c[i];
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_add_int()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for(unsigned i = 0; i < b.size(); ++i)
a[i] = b[i] + 1;
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_subtract_int()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for(unsigned i = 0; i < b.size(); ++i)
a[i] = b[i] - 1;
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_multiply()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for(unsigned k = 0; k < b.size(); ++k)
a[k] = b[k] * c[k];
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_multiply_int()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for(unsigned i = 0; i < b.size(); ++i)
a[i] = b[i] * 3;
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_divide()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for(unsigned i = 0; i < b.size(); ++i)
a[i] = b[i] / small[i];
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_divide_int()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for(unsigned i = 0; i < b.size(); ++i)
a[i] = b[i] / 3;
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_gcd()
{
using boost::integer::gcd;
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for(unsigned i = 0; i < b.size(); ++i)
a[i] = gcd(b[i], c[i]);
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_inplace_add()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for (unsigned i = 0; i < b.size(); ++i)
b[i] += c[i];
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_inplace_subtract()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for (unsigned i = 0; i < b.size(); ++i)
b[i] -= c[i];
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_inplace_add_int()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for (unsigned i = 0; i < b.size(); ++i)
b[i] += 1;
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_inplace_subtract_int()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for (unsigned i = 0; i < b.size(); ++i)
b[i] -= 1;
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_inplace_multiply()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for (unsigned k = 0; k < b.size(); ++k)
b[k] *= c[k];
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_inplace_multiply_int()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for (unsigned i = 0; i < b.size(); ++i)
b[i] *= 3;
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_inplace_divide()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for (unsigned i = 0; i < b.size(); ++i)
a[i] /= small[i];
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
double test_inplace_divide_int()
{
stopwatch<boost::chrono::high_resolution_clock> w;
for (unsigned repeats = 0; repeats < max_reps; ++repeats)
{
for (unsigned i = 0; i < b.size(); ++i)
b[i] /= 3;
}
return boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
}
private:
T generate_random(bool issmall)
{
boost::uniform_int<> ui(2, issmall ? 5 : 40), ui2(1, 10000);
std::size_t len = ui(gen);
std::vector<typename T::value_type> values;
for (std::size_t i = 0; i < len; ++i)
{
values.push_back(static_cast<typename T::value_type>(ui2(gen)));
}
return T(values.begin(), values.end());
}
std::vector<T> a, b, c, small;
static boost::random::mt19937 gen;
};
template <class N>
boost::random::mt19937 tester<N>::gen;
template <class Number>
void test(const char* type)
{
std::cout << "Testing type: " << type << std::endl;
tester<boost::math::tools::polynomial<Number> > t;
int count = 500 * max_reps;
std::string table_name = "Polynomial Arithmetic (" + compiler_name() + ", " + platform_name() + ")";
//
// Now the actual tests:
//
report_execution_time(t.test_add() / count, table_name, "operator +", type);
report_execution_time(t.test_subtract() / count, table_name, "operator -", type);
report_execution_time(t.test_multiply() / count, table_name, "operator *", type);
report_execution_time(t.test_divide() / count, table_name, "operator /", type);
report_execution_time(t.test_add_int() / count, table_name, "operator + (int)", type);
report_execution_time(t.test_subtract_int() / count, table_name, "operator - (int)", type);
report_execution_time(t.test_multiply_int() / count, table_name, "operator * (int)", type);
report_execution_time(t.test_divide_int() / count, table_name, "operator / (int)", type);
report_execution_time(t.test_inplace_add() / count, table_name, "operator +=", type);
report_execution_time(t.test_inplace_subtract() / count, table_name, "operator -=", type);
report_execution_time(t.test_inplace_multiply() / count, table_name, "operator *=", type);
report_execution_time(t.test_inplace_divide() / count, table_name, "operator /=", type);
report_execution_time(t.test_inplace_add_int() / count, table_name, "operator += (int)", type);
report_execution_time(t.test_inplace_subtract_int() / count, table_name, "operator -= (int)", type);
report_execution_time(t.test_inplace_multiply_int() / count, table_name, "operator *= (int)", type);
report_execution_time(t.test_inplace_divide_int() / count, table_name, "operator /= (int)", type);
//report_execution_time(t.test_gcd() / count, table_name, "gcd", type);
}
int main()
{
test<std::uint64_t>("std::uint64_t");
test<double>("double");
max_reps = 100;
test<boost::multiprecision::cpp_int>("cpp_int");
return 0;
}
|