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