LMMS
Loading...
Searching...
No Matches
benchmark.h
Go to the documentation of this file.
1/* Calf DSP Library
2 * Reusable performance measurement classes.
3 *
4 * Copyright (C) 2007 Krzysztof Foltman
5 *
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.
10 *
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.
15 *
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
20 */
21#ifndef __CALF_BENCHMARK_H
22#define __CALF_BENCHMARK_H
23
24#include <time.h>
25#include <sys/time.h>
26#include <sys/resource.h>
27#include <unistd.h>
28#include "primitives.h"
29#include <algorithm>
30#include <typeinfo>
31
32namespace dsp {
33#if 0
34}; to keep editor happy
35#endif
36
37
39{
40public:
41 double *data;
42 unsigned int pos, count;
43 bool sorted;
44
46 data = NULL;
47 pos = 0;
48 count = 0;
49 sorted = false;
50 }
51 void start(int items) {
52 if (data)
53 delete []data;
54 data = new double[items];
55 pos = 0;
56 count = items;
57 sorted = false;
58 }
59 void add(double value)
60 {
61 assert(pos < count);
62 data[pos++] = value;
63 }
64 void end()
65 {
66 std::sort(&data[0], &data[count]);
67 sorted = true;
68 }
69 float get()
70 {
72 return data[count >> 1];
73 }
74};
75
76// 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 USE_RDTSC 0
78#define CLOCK_SPEED (1.6 * 1000.0 * 1000.0 * 1000.0)
79
80#if USE_RDTSC
81inline uint64_t rdtsc()
82{
83 unsigned long long int x;
84 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
85 return x;
86}
87#endif
88
90{
91 static bool warned;
92};
93
94template<typename Target, class Stat>
96{
97public:
98 Target target;
99 Stat &stat;
100
101 template <class... ConstrArgs>
102 simple_benchmark(Stat &_stat, ConstrArgs... args)
103 : target(args...)
104 , stat(_stat)
105 {
106 }
107
108 void measure(int runs, int repeats)
109 {
110 int priority = getpriority(PRIO_PROCESS, getpid());
111 stat.start(runs);
112 if (setpriority(PRIO_PROCESS, getpid(), -20) < 0) {
113 if (!warned) {
114 fprintf(stderr, "Warning: could not set process priority, measurements can be worthless\n");
115 warned = true;
116 }
117 }
118 for (int i = 0; i < runs; i++) {
119 target.prepare();
120 // XXXKF measuring CPU time with clock() sucks,
121#if USE_RDTSC
122 uint64_t start = rdtsc();
123#else
124 clock_t start = ::clock();
125#endif
126 for (int j = 0; j < repeats; j++) {
127 target.run();
128 }
129#if USE_RDTSC
130 uint64_t end = rdtsc();
131 double elapsed = double(end - start) / (CLOCK_SPEED * repeats * target.scaler());
132#else
133 clock_t end = ::clock();
134 double elapsed = double(end - start) / (double(CLOCKS_PER_SEC) * repeats * target.scaler());
135#endif
136 stat.add(elapsed);
137 target.cleanup();
138 // printf("elapsed = %f start = %d end = %d diff = %d\n", elapsed, start, end, end - start);
139 }
140 setpriority(PRIO_PROCESS, getpid(), priority);
141 stat.end();
142 }
143
144 float get_stat()
145 {
146 return stat.get();
147 }
148};
149
150template<class T>
151void do_simple_benchmark(int runs = 5, int repeats = 50000)
152{
153 dsp::median_stat stat;
155
156 benchmark.measure(runs, repeats);
157
158 printf("%-30s: %f/sec, %f/CDsr, value = %f\n", typeid(T).name(), 1.0 / stat.get(), 1.0 / (44100 * stat.get()), benchmark.target.result);
159}
160
161
162#if 0
163{ to keep editor happy
164#endif
165}
166
167#endif
#define NULL
Definition CarlaBridgeFormat.cpp:30
assert(0)
#define CLOCK_SPEED
Definition benchmark.h:78
Definition benchmark.h:39
float get()
Definition benchmark.h:69
void end()
Definition benchmark.h:64
double * data
Definition benchmark.h:41
bool sorted
Definition benchmark.h:43
unsigned int count
Definition benchmark.h:42
void add(double value)
Definition benchmark.h:59
unsigned int pos
Definition benchmark.h:42
void start(int items)
Definition benchmark.h:51
median_stat()
Definition benchmark.h:45
Definition benchmark.h:96
Target target
Definition benchmark.h:98
Stat & stat
Definition benchmark.h:99
float get_stat()
Definition benchmark.h:144
void measure(int runs, int repeats)
Definition benchmark.h:108
simple_benchmark(Stat &_stat, ConstrArgs... args)
Definition benchmark.h:102
register unsigned j
Definition inflate.c:1576
register unsigned i
Definition inflate.c:1575
unsigned x[BMAX+1]
Definition inflate.c:1586
static PuglViewHint int value
Definition pugl.h:1708
static const char * name
Definition pugl.h:1582
virtual ASIOError start()=0
Definition audio_fx.h:36
void do_simple_benchmark(int runs=5, int repeats=50000)
Definition benchmark.h:151
Definition benchmark.h:90
static bool warned
Definition benchmark.h:91