2 * Reusable performance measurement classes.
4 * Copyright (C) 2007 Krzysztof Foltman
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 #ifndef __CALF_BENCHMARK_H
22 #define __CALF_BENCHMAR_H
26 #include <sys/resource.h>
28 #include "primitives.h"
34 }; to keep editor happy
42 unsigned int pos
, count
;
51 void start(int items
) {
54 data
= new double[items
];
59 void add(double value
)
66 std::sort(&data
[0], &data
[count
]);
72 return data
[count
>> 1];
76 // USE_RDTSC is for testing on my own machine, a crappy 1.6GHz Pentium 4 - it gives less headaches than clock() based measurements
78 #define CLOCK_SPEED (1.6 * 1000.0 * 1000.0 * 1000.0)
81 inline uint64_t rdtsc()
83 unsigned long long int x
;
84 __asm__
volatile (".byte 0x0f, 0x31" : "=A" (x
));
89 struct benchmark_globals
94 template<typename Target
, class Stat
>
95 class simple_benchmark
: public benchmark_globals
101 simple_benchmark(const Target
&_target
, Stat
&_stat
)
107 void measure(int runs
, int repeats
)
109 int priority
= getpriority(PRIO_PROCESS
, getpid());
111 if (setpriority(PRIO_PROCESS
, getpid(), -20) < 0) {
113 fprintf(stderr
, "Warning: could not set process priority, measurements can be worthless\n");
117 for (int i
= 0; i
< runs
; i
++) {
119 // XXXKF measuring CPU time with clock() sucks,
121 uint64_t start
= rdtsc();
123 clock_t start
= ::clock();
125 for (int j
= 0; j
< repeats
; j
++) {
129 uint64_t end
= rdtsc();
130 double elapsed
= double(end
- start
) / (CLOCK_SPEED
* repeats
* target
.scaler());
132 clock_t end
= ::clock();
133 double elapsed
= double(end
- start
) / (double(CLOCKS_PER_SEC
) * repeats
* target
.scaler());
137 // printf("elapsed = %f start = %d end = %d diff = %d\n", elapsed, start, end, end - start);
139 setpriority(PRIO_PROCESS
, getpid(), priority
);
150 void do_simple_benchmark(int runs
= 5, int repeats
= 50000)
152 dsp::median_stat stat
;
153 dsp::simple_benchmark
<T
, dsp::median_stat
> benchmark(T(), stat
);
155 benchmark
.measure(runs
, repeats
);
157 printf("%-30s: %f/sec, %f/CDsr, value = %f\n", typeid(T
).name(), 1.0 / stat
.get(), 1.0 / (44100 * stat
.get()), benchmark
.target
.result
);
162 { to keep editor happy