Imported Upstream version 3.0
[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 2, 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
34 #define ITERATIONS      5000000
35 #define BLOCK_SIZE      (10 * 1000)     // fits in cache
36
37 #define FREQ    5003.123
38 #define K       4.9999999
39 #define AMPLITUDE       2.444444444
40
41
42 static double
43 timeval_to_double (const struct timeval *tv)
44 {
45   return (double) tv->tv_sec + (double) tv->tv_usec * 1e-6;
46 }
47
48
49 static void
50 benchmark (void test (float *x, const float *y), const char *implementation_name)
51 {
52 #ifdef HAVE_SYS_RESOURCE_H
53   struct rusage rusage_start;
54   struct rusage rusage_stop;
55 #else
56   double clock_start;
57   double clock_end;
58 #endif
59   float output[BLOCK_SIZE];
60   float input[BLOCK_SIZE];
61
62   // touch memory
63   memset(output, 0, BLOCK_SIZE*sizeof(float));
64   for (int i = 0; i<BLOCK_SIZE; i++)
65     input[i] = sin(i);
66
67   // get starting CPU usage
68 #ifdef HAVE_SYS_RESOURCE_H
69   if (getrusage (RUSAGE_SELF, &rusage_start) < 0){
70     perror ("getrusage");
71     exit (1);
72   }
73 #else
74   clock_start = (double) clock() * (1000000. / CLOCKS_PER_SEC);
75 #endif
76   // do the actual work
77
78   test (output, input);
79
80   // get ending CPU usage
81
82 #ifdef HAVE_SYS_RESOURCE_H
83   if (getrusage (RUSAGE_SELF, &rusage_stop) < 0){
84     perror ("getrusage");
85     exit (1);
86   }
87
88   // compute results
89
90   double user =
91     timeval_to_double (&rusage_stop.ru_utime)
92     - timeval_to_double (&rusage_start.ru_utime);
93
94   double sys =
95     timeval_to_double (&rusage_stop.ru_stime)
96     - timeval_to_double (&rusage_start.ru_stime);
97
98   double total = user + sys;
99 #else
100   clock_end = (double) clock () * (1000000. / CLOCKS_PER_SEC);
101   double total = clock_end - clock_start;
102 #endif
103
104   printf ("%18s:  cpu: %6.3f  steps/sec: %10.3e\n",
105           implementation_name, total, ITERATIONS / total);
106 }
107
108 // ----------------------------------------------------------------
109
110 void basic_vco (float *output, const float *input)
111 {
112   double phase = 0;
113
114   for (int j = 0; j < ITERATIONS/BLOCK_SIZE; j++){
115     for (int i = 0; i < BLOCK_SIZE; i++){
116       output[i] = cos(phase) * AMPLITUDE;
117       phase += input[i] * K;
118
119       while (phase > 2 * M_PI)
120         phase -= 2 * M_PI;
121
122       while (phase < -2 * M_PI)
123         phase += 2 * M_PI;
124     }
125   }
126 }
127
128 void native_vco (float *output, const float *input)
129 {
130   gr_vco<float,float>   vco;
131
132   for (int j = 0; j < ITERATIONS/BLOCK_SIZE; j++){
133     vco.cos(output, input, BLOCK_SIZE, K, AMPLITUDE);
134   }
135     }
136
137 void fxpt_vco (float *output, const float *input)
138 {
139   gr_fxpt_vco   vco;
140
141   for (int j = 0; j < ITERATIONS/BLOCK_SIZE; j++){
142     vco.cos(output, input, BLOCK_SIZE, K, AMPLITUDE);
143   }
144 }
145
146 // ----------------------------------------------------------------
147
148 void nop_fct (float *x, const float *y)
149 {
150 }
151
152 void nop_loop (float *x, const float *y)
153 {
154   for (int i = 0; i < ITERATIONS; i++){
155     nop_fct (x, y);
156   }
157 }
158
159 int
160 main (int argc, char **argv)
161 {
162   benchmark (nop_loop, "nop loop");
163   benchmark (basic_vco, "basic vco");
164   benchmark (native_vco, "native vco");
165   benchmark (fxpt_vco, "fxpt vco");
166 }