summaryrefslogtreecommitdiff
path: root/libs/math/performance/bernoulli_performance.cpp
blob: e21e8800e02c496059331f7093938f87125a1270 (plain)
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
//  (C) Copyright John Maddock 2013.
//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/math/special_functions/bernoulli.hpp>
#include <boost/chrono.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>

template <class Clock>
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;
};

void time()
{
   stopwatch<boost::chrono::high_resolution_clock> w;
   //
   // Sum the first 1000 Bernoulli numbers:
   //
   boost::multiprecision::cpp_dec_float_50 sum = 0;
   for(unsigned i = 0; i < 1000; ++i)
   {
      sum += boost::math::bernoulli_b2n<boost::multiprecision::cpp_dec_float_50>(i);
   }
   double t = boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count();
   std::cout << "Total execution time = " << std::setprecision(3) << t << "s" << std::endl;
   std::cout << "Sum was: " << sum << std::endl;
}

int main()
{
   // Call time() twice: first call results in the cache being populated, second used already cached values:
   time();
   time();
   return 0;
}

/*

No atomic int:

Total execution time = 0.646s
Sum was: 2.86e+4134
Total execution time = 0.000341s
Sum was: 2.86e+4134

With atomic int:

Total execution time = 0.643s
Sum was: 2.86e+4134
Total execution time = 0.000309s
Sum was: 2.86e+4134

No threads:

Total execution time = 0.652s
Sum was: 2.86e+4134
Total execution time = 0.000281s
Sum was: 2.86e+4134

*/