// performance.cpp : Defines the entry point for the console application. // #include #include #include #include #include #include #include typedef boost::numeric::safe safe_type; namespace boost { namespace multiprecision { namespace detail { template struct double_integer; template <> struct double_integer { typedef boost::numeric::safe type; }; } template typename enable_if_c::value, Integer&>::type multiply(Integer& result, const I2& a, const I2& b) { return result = static_cast(a) * static_cast(b); } template typename enable_if_c::value, bool>::type bit_test(const Integer& val, unsigned index) { Integer mask = 1; if (index >= sizeof(Integer) * CHAR_BIT) return 0; if (index) mask <<= index; return val & mask ? true : false; } template typename enable_if_c::value, I2>::type integer_modulus(const I1& x, I2 val) { return static_cast(x % val); } template typename enable_if_c::value, I1>::type powm(const I1& a, I2 b, I3 c) { typedef typename detail::double_integer::type double_type; I1 x(1), y(a); double_type result; while (b > 0) { if (b & 1) { multiply(result, x, y); x = integer_modulus(result, c); } multiply(result, y, y); y = integer_modulus(result, c); b >>= 1; } return x % c; } template inline unsigned lsb(const boost::numeric::safe& x) { return lsb(static_cast(x)); } } } #include template struct stopwatch { typedef typename Clock::duration duration; stopwatch() { m_start = Clock::now(); } duration elapsed() { return Clock::now() - m_start; } void reset() { m_start = Clock::now(); } private: typename Clock::time_point m_start; }; int main() { boost::chrono::duration time; stopwatch c; unsigned count = 0; for (unsigned i = 3; i < 30000000; ++i) if (boost::multiprecision::miller_rabin_test(i, 25)) ++count; time = c.elapsed(); std::cout << "Testing type unsigned:\ntime = " << time; std::cout << "\ncount = " << count << std::endl; c.reset(); count = 0; for (safe_type i = 3; i < 30000000; ++i) if (boost::multiprecision::miller_rabin_test(i, 25)) ++count; time = c.elapsed(); std::cout << "Testing type safe:\ntime = " << time; std::cout << "\ncount = " << count << std::endl; c.reset(); count = 0; typedef boost::multiprecision::number > safe_mp_type; for (safe_mp_type i = 3; i < 30000000; ++i) if (boost::multiprecision::miller_rabin_test(i, 25)) ++count; time = c.elapsed(); std::cout << "Testing type 32-bit cpp_int:\ntime = " << time; std::cout << "\ncount = " << count << std::endl; return 0; }