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., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 #ifndef __CALF_BENCHMARK_H
22 #define __CALF_BENCHMAR_H
26 #include <sys/resource.h>
27 #include "primitives.h"
33 }; to keep editor happy
41 unsigned int pos
, count
;
50 void start(int items
) {
53 data
= new double[items
];
58 void add(double value
)
65 std::sort(&data
[0], &data
[count
]);
71 return data
[count
>> 1];
75 // USE_RDTSC is for testing on my own machine, a crappy 1.6GHz Pentium 4 - it gives less headaches than clock() based measurements
77 #define CLOCK_SPEED (1.6 * 1000.0 * 1000.0 * 1000.0)
80 inline uint64_t rdtsc()
82 unsigned long long int x
;
83 __asm__
volatile (".byte 0x0f, 0x31" : "=A" (x
));
88 struct benchmark_globals
93 template<typename Target
, class Stat
>
94 class simple_benchmark
: public benchmark_globals
100 simple_benchmark(const Target
&_target
, Stat
&_stat
)
106 void measure(int runs
, int repeats
)
108 int priority
= getpriority(PRIO_PROCESS
, getpid());
110 if (setpriority(PRIO_PROCESS
, getpid(), -20) < 0) {
112 fprintf(stderr
, "Warning: could not set process priority, measurements can be worthless\n");
116 for (int i
= 0; i
< runs
; i
++) {
118 // XXXKF measuring CPU time with clock() sucks,
120 uint64_t start
= rdtsc();
122 clock_t start
= ::clock();
124 for (int j
= 0; j
< repeats
; j
++) {
128 uint64_t end
= rdtsc();
129 double elapsed
= double(end
- start
) / (CLOCK_SPEED
* repeats
* target
.scaler());
131 clock_t end
= ::clock();
132 double elapsed
= double(end
- start
) / (double(CLOCKS_PER_SEC
) * repeats
* target
.scaler());
136 // printf("elapsed = %f start = %d end = %d diff = %d\n", elapsed, start, end, end - start);
138 setpriority(PRIO_PROCESS
, getpid(), priority
);
149 void do_simple_benchmark(int runs
= 5, int repeats
= 50000)
151 dsp::median_stat stat
;
152 dsp::simple_benchmark
<T
, dsp::median_stat
> benchmark(T(), stat
);
154 benchmark
.measure(runs
, repeats
);
156 printf("%-30s: %f/sec, %f/CDsr, value = %f\n", typeid(T
).name(), 1.0 / stat
.get(), 1.0 / (44100 * stat
.get()), benchmark
.target
.result
);
161 { to keep editor happy