Debian Codegraph @

benchmark.py

raw symbols SCIP
1 #!/usr/bin/env python
2 from __future__ import print_function, division
3
4 import sys
5 import timeit
6
7 args = sys.argv[1:]
8 if len(args) == 0:
9 args = ['ccm89', 'fitzpatrick99', 'apply']
10
11 if 'ccm89' in args:
12 setup = """
13 import numpy
14 from extinction import {name}
15 wave = numpy.logspace(numpy.log10({minwave}),
16 numpy.log10({maxwave}),
17 {size:d})
18 """
19
20 stmt = "{name}(wave, 1.0, 3.1)"
21
22 benchmarks = [
23 {'name': 'ccm89',
24 'minwave': 1000., 'maxwave': 30000., 'size': 10, 'nloops': 1000000},
25 {'name': 'ccm89',
26 'minwave': 1000., 'maxwave': 30000., 'size': 100, 'nloops': 100000},
27 {'name': 'ccm89',
28 'minwave': 1000., 'maxwave': 30000., 'size': 1000, 'nloops': 10000},
29 {'name': 'ccm89',
30 'minwave': 1000., 'maxwave': 30000., 'size': 10000, 'nloops': 1000},
31 {'name': 'odonnell94',
32 'minwave': 1000., 'maxwave': 30000., 'size': 100, 'nloops': 100000},
33 {'name': 'ccm89',
34 'minwave': 1000., 'maxwave': 1e4/8, 'size': 100, 'nloops': 100000},
35 {'name': 'ccm89',
36 'minwave': 1e4/8, 'maxwave': 1e4/3.3, 'size': 100, 'nloops': 100000},
37 {'name': 'ccm89',
38 'minwave': 1e4/3.3, 'maxwave': 1e4/1.1, 'size': 100, 'nloops': 100000},
39 {'name': 'ccm89',
40 'minwave': 1e4/1.1, 'maxwave': 1e4/0.3, 'size': 100, 'nloops': 100000}]
41 for b in benchmarks:
42 times = timeit.repeat(stmt.format(**b), setup=setup.format(**b),
43 repeat=3, number=b['nloops'])
44 b['time'] = min(times) / b['nloops'] * 1e6
45 print('{name:5s} wave=[{minwave: 7.1f}, {maxwave: 7.1f}] size={size:5d} {time:8.2f}us'.format(**b))
46
47
48 print()
49
50 if 'fitzpatrick99' in args:
51
52 # -----------------------------------------------------------------------
53 # Fitzpatrick init
54 setup = """
55 import numpy
56 from extinction import Fitzpatrick99
57 """
58
59 stmt = "Fitzpatrick99(3.1)"
60
61 nloops = 10000
62 times = timeit.repeat(stmt, setup=setup, repeat=3, number=nloops)
63 t = min(times) / nloops * 1e6
64 print('F99 init {:8.2f}us'.format(t))
65
66
67 #------------------------------------------------------------------------
68 # Fitzpatrick fast method
69
70 setup = """
71 import numpy
72 from extinction import Fitzpatrick99
73
74 wave = numpy.logspace(numpy.log10({minwave}),
75 numpy.log10({maxwave}),
76 {size:d})
77 f = Fitzpatrick99(3.1)
78 """
79
80 stmt = "f(wave, 1.0)"
81
82 benchmarks = [
83 {'minwave': 1000., 'maxwave': 30000., 'size': 10, 'nloops': 10000},
84 {'minwave': 1000., 'maxwave': 30000., 'size': 100, 'nloops': 10000},
85 {'minwave': 1000., 'maxwave': 30000., 'size': 1000, 'nloops': 10000},
86 {'minwave': 1000., 'maxwave': 30000., 'size': 10000, 'nloops': 1000},
87 {'minwave': 1000., 'maxwave': 2700., 'size': 1000, 'nloops': 10000},
88 {'minwave': 2700., 'maxwave': 30000., 'size': 1000, 'nloops': 10000}]
89 for b in benchmarks:
90 times = timeit.repeat(stmt.format(**b), setup=setup.format(**b),
91 repeat=3, number=b['nloops'])
92 b['time'] = min(times) / b['nloops'] * 1e6
93 print('Fitzpatrick99 call wave=[{minwave: 7.1f}, {maxwave: 7.1f}] size={size:5d} {time:8.2f}us'.format(**b))
94
95
96 if 'apply' in args:
97
98 setup = """
99 import numpy
100 from extinction import apply
101 ext = numpy.ones({size:d})
102 flux = numpy.ones({size:d})
103 """
104
105 stmt = "apply(ext, flux, inplace={inplace!r})"
106
107 benchmarks = [
108 {'size': 10, 'nloops': 100000, 'inplace': True},
109 {'size': 10, 'nloops': 100000, 'inplace': False},
110 {'size': 100, 'nloops': 100000, 'inplace': True},
111 {'size': 100, 'nloops': 100000, 'inplace': False},
112 {'size': 1000, 'nloops': 10000, 'inplace': False}]
113 for b in benchmarks:
114 times = timeit.repeat(stmt.format(**b), setup=setup.format(**b),
115 repeat=3, number=b['nloops'])
116 b['time'] = min(times) / b['nloops'] * 1e6
117 print('apply size={size:5d} inplace={inplace!r} {time:8.2f}us'.format(**b))
118
119
120 #def benchmark(f, args, repeat=3):
121 # """Time `f(*args)`, repeat `repeat` times and return the best time."""
122 #
123 # number = 1
124 # times = []
125 #
126 # # determine number of times to run the loop.
127 # while number < 10**9:
128 # t0 = time.time()
129 # for i in range(number):
130 # f(*args)
131 # t = time.time() - t0
132 #
133 # if t > 0.1:
134 # break
135 #
136 # number *= 10
137 #
138 # # run the loop `number` times
139