1 //****************************************************************************//
2 //       Copyright (C) 2014 Florent Hivert <Florent.Hivert@lri.fr>,           //
3 //                                                                            //
4 //  Distributed under the terms of the GNU General Public License (GPL)       //
5 //                                                                            //
6 //    This code is distributed in the hope that it will be useful,            //
7 //    but WITHOUT ANY WARRANTY; without even the implied warranty of          //
8 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       //
9 //   General Public License for more details.                                 //
10 //                                                                            //
11 //  The full text of the GPL is available at:                                 //
12 //                                                                            //
13 //                  http://www.gnu.org/licenses/                              //
14 //****************************************************************************//
15 
16 #ifndef HPCOMBI_TESTTOOLS_HPP_INCLUDED
17 #define HPCOMBI_TESTTOOLS_HPP_INCLUDED
18 
19 #include <chrono>
20 #include <iomanip>
21 #include <iostream>
22 #include <vector>
23 
24 #include "perm16.hpp"
25 namespace HPCombi {
26 
factorial(unsigned int n)27 constexpr unsigned int factorial(unsigned int n) {
28     return n > 1 ? n * factorial(n - 1) : 1;
29 }
30 
rand_perm()31 inline epu8 rand_perm() {
32     epu8 res = HPCombi::epu8id;
33     auto &ar = HPCombi::as_array(res);
34     std::random_shuffle(ar.begin(), ar.end());
35     return res;
36 }
37 
rand_perms(int sz)38 inline std::vector<epu8> rand_perms(int sz) {
39     std::vector<epu8> res(sz);
40     std::srand(std::time(0));
41     for (int i = 0; i < sz; i++)
42         res[i] = rand_perm();
43     return res;
44 }
45 
46 // std::vector<Perm16> all_perms(int n);
47 
48 // using a template allows us to ignore the differences between functors,
49 // function pointers and lambda
50 template <typename Func>
timethat(Func fun,int rep=1,double reftime=0)51 double timethat(Func fun, int rep = 1, double reftime = 0) {
52     using std::chrono::duration;
53     using std::chrono::duration_cast;
54     using std::chrono::high_resolution_clock;
55     auto tstart = high_resolution_clock::now();
56     for (int i = 0; i < rep; i++)
57         fun();
58     auto tfin = high_resolution_clock::now();
59 
60     auto tm = duration_cast<duration<double>>(tfin - tstart);
61     std::cout << "time = " << std::fixed << std::setprecision(6) << tm.count()
62               << "s";
63     if (reftime != 0)
64         std::cout << ", speedup = " << std::setprecision(3)
65                   << reftime / tm.count();
66     std::cout << std::endl;
67     return tm.count();
68 }
69 
70 }  // namespace HPCombi
71 #endif  // HPCOMBI_TESTTOOLS_HPP_INCLUDED
72