]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/performance/rational_bernoulli_bench.cpp
1 // Copyright 2020 John Maddock. Distributed under the Boost
2 // Software License, Version 1.0. (See accompanying file
3 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
6 #include <benchmark/benchmark.h>
7 #include <boost/multiprecision/cpp_int.hpp>
8 #include <boost/multiprecision/gmp.hpp>
12 template <class Integer
>
13 inline Integer
factorial(unsigned n
)
16 for (unsigned k
= 1; k
<= n
; ++k
)
21 template <class Rational
, class Integer
= typename
Rational::value_type
>
22 inline Rational
binomial(unsigned n
, unsigned k
)
24 return Rational(factorial
<Integer
>(n
), factorial
<Integer
>(k
) * factorial
<Integer
>(n
- k
));
27 inline mpz_class
pow(mpz_class i
, unsigned p
)
30 mpz_pow_ui(result
.get_mpz_t(), i
.get_mpz_t(), p
);
34 template <class Rational
, class Integer
= typename
Rational::value_type
>
35 Rational
Bernoulli(unsigned m
)
39 for (unsigned k
= 0; k
<= m
; ++k
)
42 for (unsigned v
= 0; v
<= k
; ++v
)
44 Rational term
= binomial
<Rational
, Integer
>(k
, v
) * Rational(pow(Integer(v
), m
), k
+ 1);
54 template <class Rational
, class Integer
= typename
Rational::value_type
>
55 static void BM_bernoulli(benchmark::State
& state
)
57 int m
= state
.range(0);
60 benchmark::DoNotOptimize(Bernoulli
<Rational
, Integer
>(m
));
65 BENCHMARK_TEMPLATE(BM_bernoulli
, boost::multiprecision::cpp_rational
)->DenseRange(50, 200, 4);
66 BENCHMARK_TEMPLATE(BM_bernoulli
, boost::multiprecision::mpq_rational
)->DenseRange(50, 200, 4);
67 BENCHMARK_TEMPLATE(BM_bernoulli
, boost::multiprecision::number
<boost::multiprecision::rational_adaptor
<boost::multiprecision::gmp_int
> >)->DenseRange(50, 200, 4);
68 BENCHMARK_TEMPLATE(BM_bernoulli
, mpq_class
, mpz_class
)->DenseRange(50, 200, 4);