1 //  some benchmarking helpers
2 //  Copyright (C) 2008 Tim Blechmann
3 //
4 //  This program is free software; you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation; either version 2 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; see the file COPYING.  If not, write to
16 //  the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
17 //  Boston, MA 02110-1301, USA.
18 
19 #include <iostream>
20 #include <stdint.h>
21 
22 #include <boost/bind.hpp>
23 
24 #include "cache_aligned_array.hpp"
25 
26 template <typename Container>
fill_container(Container & t)27 void fill_container(Container & t)
28 {
29     for (int i = 0; i != t.size(); ++i)
30         t[i] = rand()/RAND_MAX * 2 - 1;
31 }
32 
33 
34 #ifdef NO_PERFORMANCE_COUNTERS
35 
36 #include "../source/utilities/high_resolution_timer.hpp"
37 
38 template <typename Function>
run_bench(Function const & f,unsigned int loops=5000000)39 inline void run_bench(Function const & f, unsigned int loops = 5000000)
40 {
41     boost::high_resolution_timer hrt;
42     hrt.restart();
43 
44     for (int i = 0; i != loops; ++i)
45         f();
46 
47     double e = hrt.elapsed();
48     std::cout << e << std::endl;
49 }
50 
51 #else
52 
53 #include "perf_counter.hpp"
54 
55 template <typename Function>
run_bench(Function const & f,unsigned int loops=5000000)56 inline void run_bench(Function const & f, unsigned int loops = 5000000)
57 {
58     perf_counter pc;
59     pc.start();
60 
61     for (int i = 0; i != loops; ++i)
62         f();
63 
64     pc.stop();
65     pc.dump(false);
66 }
67 
68 #endif
69 
70 #define __noinline__ __attribute__ ((noinline))
71