Imported Upstream version 3.0.4
[debian/gnuradio] / gnuradio-core / src / tests / benchmark_nco.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,2004 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_nco.h>
32 #include <gr_fxpt_nco.h>
33
34 #define ITERATIONS      20000000
35 #define BLOCK_SIZE      (10 * 1000)     // fits in cache
36
37 #define FREQ    5003.123
38
39 static double
40 timeval_to_double (const struct timeval *tv)
41 {
42   return (double) tv->tv_sec + (double) tv->tv_usec * 1e-6;
43 }
44
45
46 static void
47 benchmark (void test (float *x, float *y), const char *implementation_name)
48 {
49 #ifdef HAVE_SYS_RESOURCE_H
50   struct rusage rusage_start;
51   struct rusage rusage_stop;
52 #else
53   double clock_start;
54   double clock_end;
55 #endif
56   float output[2*BLOCK_SIZE];
57   float *x = &output[0], *y = &output[BLOCK_SIZE];
58
59   // touch memory
60   memset(output, 0, 2*BLOCK_SIZE*sizeof(float));
61
62   // get starting CPU usage
63 #ifdef HAVE_SYS_RESOURCE_H
64   if (getrusage (RUSAGE_SELF, &rusage_start) < 0){
65     perror ("getrusage");
66     exit (1);
67   }
68 #else
69   clock_start = (double) clock() * (1000000. / CLOCKS_PER_SEC);
70 #endif
71   // do the actual work
72
73   test (x, y);
74
75   // get ending CPU usage
76
77 #ifdef HAVE_SYS_RESOURCE_H
78   if (getrusage (RUSAGE_SELF, &rusage_stop) < 0){
79     perror ("getrusage");
80     exit (1);
81   }
82
83   // compute results
84
85   double user =
86     timeval_to_double (&rusage_stop.ru_utime)
87     - timeval_to_double (&rusage_start.ru_utime);
88
89   double sys =
90     timeval_to_double (&rusage_stop.ru_stime)
91     - timeval_to_double (&rusage_start.ru_stime);
92
93   double total = user + sys;
94 #else
95   clock_end = (double) clock () * (1000000. / CLOCKS_PER_SEC);
96   double total = clock_end - clock_start;
97 #endif
98
99   printf ("%18s:  cpu: %6.3f  steps/sec: %10.3e\n",
100           implementation_name, total, ITERATIONS / total);
101 }
102
103 // ----------------------------------------------------------------
104 // Don't compare the _vec with other functions since memory store's
105 // are involved.
106
107 void basic_sincos_vec (float *x, float *y)
108 {
109   gr_nco<float,float>   nco;
110
111   nco.set_freq (2 * M_PI / FREQ);
112
113   for (int i = 0; i < ITERATIONS/BLOCK_SIZE; i++){
114     for (int j = 0; j < BLOCK_SIZE; j++){
115       nco.sincos (&x[2*j+1], &x[2*j]);
116       nco.step ();
117     }
118   }
119 }
120
121 void native_sincos_vec (float *x, float *y)
122 {
123   gr_nco<float,float>   nco;
124
125   nco.set_freq (2 * M_PI / FREQ);
126  
127   for (int i = 0; i < ITERATIONS/BLOCK_SIZE; i++){
128     nco.sincos ((gr_complex*)x, BLOCK_SIZE);
129   }
130 }
131
132 void fxpt_sincos_vec (float *x, float *y)
133 {
134   gr_fxpt_nco   nco;
135   
136   nco.set_freq (2 * M_PI / FREQ);
137   
138   for (int i = 0; i < ITERATIONS/BLOCK_SIZE; i++){
139     nco.sincos ((gr_complex*)x, BLOCK_SIZE);
140   }
141 }
142
143 // ----------------------------------------------------------------
144
145 void native_sincos (float *x, float *y)
146 {
147   gr_nco<float,float>   nco;
148
149   nco.set_freq (2 * M_PI / FREQ);
150
151   for (int i = 0; i < ITERATIONS; i++){
152     nco.sincos (x, y);
153     nco.step ();
154   }
155 }
156
157 void fxpt_sincos (float *x, float *y)
158 {
159   gr_fxpt_nco   nco;
160
161   nco.set_freq (2 * M_PI / FREQ);
162
163   for (int i = 0; i < ITERATIONS; i++){
164     nco.sincos (x, y);
165     nco.step ();
166   }
167 }
168
169 // ----------------------------------------------------------------
170
171 void native_sin (float *x, float *y)
172 {
173   gr_nco<float,float>   nco;
174
175   nco.set_freq (2 * M_PI / FREQ);
176
177   for (int i = 0; i < ITERATIONS; i++){
178     *x = nco.sin ();
179     nco.step ();
180   }
181 }
182
183 void fxpt_sin (float *x, float *y)
184 {
185   gr_fxpt_nco   nco;
186
187   nco.set_freq (2 * M_PI / FREQ);
188
189   for (int i = 0; i < ITERATIONS; i++){
190     *x = nco.sin ();
191     nco.step ();
192   }
193 }
194
195 // ----------------------------------------------------------------
196
197 void nop_fct (float *x, float *y)
198 {
199 }
200
201 void nop_loop (float *x, float *y)
202 {
203   for (int i = 0; i < ITERATIONS; i++){
204     nop_fct (x, y);
205   }
206 }
207
208 int
209 main (int argc, char **argv)
210 {
211   benchmark (nop_loop, "nop loop");
212   benchmark (native_sin, "native sine");
213   benchmark (fxpt_sin, "fxpt sine");
214   benchmark (native_sincos, "native sin/cos");
215   benchmark (fxpt_sincos, "fxpt sin/cos");
216   benchmark (basic_sincos_vec, "basic sin/cos vec");
217   benchmark (native_sincos_vec, "native sin/cos vec");
218   benchmark (fxpt_sincos_vec, "fxpt sin/cos vec");
219 }