Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / tests / benchmark_vco.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,2004,2005 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <stdio.h>
26 #include <sys/time.h>
27 #ifdef HAVE_SYS_RESOURCE_H
28 #include <sys/resource.h>
29 #endif
30 #include <unistd.h>
31 #include <gr_vco.h>
32 #include <gr_fxpt_vco.h>
33 #include <string.h>
34
35 #define ITERATIONS      5000000
36 #define BLOCK_SIZE      (10 * 1000)     // fits in cache
37
38 #define FREQ    5003.123
39 #define K       4.9999999
40 #define AMPLITUDE       2.444444444
41
42
43 static double
44 timeval_to_double (const struct timeval *tv)
45 {
46   return (double) tv->tv_sec + (double) tv->tv_usec * 1e-6;
47 }
48
49
50 static void
51 benchmark (void test (float *x, const float *y), const char *implementation_name)
52 {
53 #ifdef HAVE_SYS_RESOURCE_H
54   struct rusage rusage_start;
55   struct rusage rusage_stop;
56 #else
57   double clock_start;
58   double clock_end;
59 #endif
60   float output[BLOCK_SIZE];
61   float input[BLOCK_SIZE];
62
63   // touch memory
64   memset(output, 0, BLOCK_SIZE*sizeof(float));
65   for (int i = 0; i<BLOCK_SIZE; i++)
66     input[i] = sin(i);
67
68   // get starting CPU usage
69 #ifdef HAVE_SYS_RESOURCE_H
70   if (getrusage (RUSAGE_SELF, &rusage_start) < 0){
71     perror ("getrusage");
72     exit (1);
73   }
74 #else
75   clock_start = (double) clock() * (1000000. / CLOCKS_PER_SEC);
76 #endif
77   // do the actual work
78
79   test (output, input);
80
81   // get ending CPU usage
82
83 #ifdef HAVE_SYS_RESOURCE_H
84   if (getrusage (RUSAGE_SELF, &rusage_stop) < 0){
85     perror ("getrusage");
86     exit (1);
87   }
88
89   // compute results
90
91   double user =
92     timeval_to_double (&rusage_stop.ru_utime)
93     - timeval_to_double (&rusage_start.ru_utime);
94
95   double sys =
96     timeval_to_double (&rusage_stop.ru_stime)
97     - timeval_to_double (&rusage_start.ru_stime);
98
99   double total = user + sys;
100 #else
101   clock_end = (double) clock () * (1000000. / CLOCKS_PER_SEC);
102   double total = clock_end - clock_start;
103 #endif
104
105   printf ("%18s:  cpu: %6.3f  steps/sec: %10.3e\n",
106           implementation_name, total, ITERATIONS / total);
107 }
108
109 // ----------------------------------------------------------------
110
111 void basic_vco (float *output, const float *input)
112 {
113   double phase = 0;
114
115   for (int j = 0; j < ITERATIONS/BLOCK_SIZE; j++){
116     for (int i = 0; i < BLOCK_SIZE; i++){
117       output[i] = cos(phase) * AMPLITUDE;
118       phase += input[i] * K;
119
120       while (phase > 2 * M_PI)
121         phase -= 2 * M_PI;
122
123       while (phase < -2 * M_PI)
124         phase += 2 * M_PI;
125     }
126   }
127 }
128
129 void native_vco (float *output, const float *input)
130 {
131   gr_vco<float,float>   vco;
132
133   for (int j = 0; j < ITERATIONS/BLOCK_SIZE; j++){
134     vco.cos(output, input, BLOCK_SIZE, K, AMPLITUDE);
135   }
136     }
137
138 void fxpt_vco (float *output, const float *input)
139 {
140   gr_fxpt_vco   vco;
141
142   for (int j = 0; j < ITERATIONS/BLOCK_SIZE; j++){
143     vco.cos(output, input, BLOCK_SIZE, K, AMPLITUDE);
144   }
145 }
146
147 // ----------------------------------------------------------------
148
149 void nop_fct (float *x, const float *y)
150 {
151 }
152
153 void nop_loop (float *x, const float *y)
154 {
155   for (int i = 0; i < ITERATIONS; i++){
156     nop_fct (x, y);
157   }
158 }
159
160 int
161 main (int argc, char **argv)
162 {
163   benchmark (nop_loop, "nop loop");
164   benchmark (basic_vco, "basic vco");
165   benchmark (native_vco, "native vco");
166   benchmark (fxpt_vco, "fxpt vco");
167 }