Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / benchmark_filters.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006,2007 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
23 import time
24 import random
25 from optparse import OptionParser
26 from gnuradio import gr
27 from gnuradio.eng_option import eng_option
28
29 def make_random_complex_tuple(L):
30     result = []
31     for x in range(L):
32         result.append(complex(random.uniform(-1000,1000),
33                               random.uniform(-1000,1000)))
34     return tuple(result)
35     
36 def benchmark(name, creator, dec, ntaps, total_test_size, block_size):
37     block_size = 32768
38
39     tb = gr.top_block()
40     taps = make_random_complex_tuple(ntaps)
41     src = gr.vector_source_c(make_random_complex_tuple(block_size), True)
42     head = gr.head(gr.sizeof_gr_complex, int(total_test_size))
43     op = creator(dec, taps)
44     dst = gr.null_sink(gr.sizeof_gr_complex)
45     tb.connect(src, head, op, dst)
46     start = time.time()
47     tb.run()
48     stop = time.time()
49     delta = stop - start
50     print "%16s: taps: %4d  input: %4g, time: %6.3f  taps/sec: %10.4g" % (
51         name, ntaps, total_test_size, delta, ntaps*total_test_size/delta)
52
53 def main():
54     parser = OptionParser(option_class=eng_option)
55     parser.add_option("-n", "--ntaps", type="int", default=256)
56     parser.add_option("-t", "--total-input-size", type="eng_float", default=40e6)
57     parser.add_option("-b", "--block-size", type="intx", default=50000)
58     parser.add_option("-d", "--decimation", type="int", default=1)
59     (options, args) = parser.parse_args()
60     if len(args) != 0:
61         parser.print_help()
62         sys.exit(1)
63
64     ntaps = options.ntaps
65     total_input_size = options.total_input_size
66     block_size = options.block_size
67     dec = options.decimation
68
69     benchmark("gr.fir_filter_ccc", gr.fir_filter_ccc,
70               dec, ntaps, total_input_size, block_size)
71     benchmark("gr.fft_filter_ccc", gr.fft_filter_ccc,
72               dec, ntaps, total_input_size, block_size)
73
74 if __name__ == '__main__':
75     main()