1 // Copyright John Maddock 2015.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef PERFORMANCE_HPP
7 #define PERFORMANCE_HPP
9 #include <boost/math/special_functions/relative_difference.hpp>
10 #include <boost/array.hpp>
11 #include <boost/chrono.hpp>
12 #include <boost/regex.hpp>
14 template <class Array>
15 void add_data(const Array& a)
18 // This function is called multiple times to merge multiple data sets into one big table:
20 for(typename Array::const_iterator i = a.begin(); i != a.end(); ++i)
22 data.push_back(std::vector<double>());
23 for(typename Array::value_type::const_iterator j = i->begin(); j != i->end(); ++j)
25 data.back().push_back(*j);
30 template <class Func, class Result>
31 void screen_data(Func f, Result r)
34 // If any of the implementations being tested produces garbage for one of our
35 // test cases (or else if we test a domain they don't support), then we remove that
36 // row from the table. This allows us to only test a common supported sub-set for performance:
38 for(std::vector<std::vector<double> >::size_type row = 0; row < data.size(); ++row)
42 double computed = f(data[row]);
43 double expected = r(data[row]);
44 double err = boost::math::relative_difference(computed, expected);
47 std::cout << "Erasing row: ";
48 for(unsigned i = 0; i < data[row].size(); ++i)
50 std::cout << data[row][i] << " ";
52 std::cout << "Error was " << err << std::endl;
53 data.erase(data.begin() + row);
57 catch(const std::exception& e)
59 std::cout << "Erasing row: ";
60 for(unsigned i = 0; i < data[row].size(); ++i)
62 std::cout << data[row][i] << " ";
64 std::cout << "due to thrown exception: " << e.what() << std::endl;
65 data.erase(data.begin() + row);
71 template <class Clock>
74 typedef typename Clock::duration duration;
77 m_start = Clock::now();
81 return Clock::now() - m_start;
85 m_start = Clock::now();
89 typename Clock::time_point m_start;
95 double exec_timed_test(Func f)
100 stopwatch<boost::chrono::high_resolution_clock> w;
102 for(unsigned count = 0; count < repeats; ++count)
104 for(std::vector<std::vector<double> >::const_iterator i = data.begin(); i != data.end(); ++i)
108 t = boost::chrono::duration_cast<boost::chrono::duration<double>>(w.elapsed()).count();
112 return t / (repeats * data.size());
115 #endif // PERFORMANCE_HPP