-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathslow_average.py
52 lines (36 loc) · 1.15 KB
/
slow_average.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
"""
A dummy script which could run much faster.
Try to profile it, find bad design and correct it!
"""
import logging
import numpy as np
from itertools import product
from collections import namedtuple
from contexttimer import timer
# will be modified, this is is not thread safe!
cache = namedtuple('Cache', ('keys', 'values'))
cache.keys = []
cache.values = []
def read_wind_speed(time, location_x, location_y):
"""Read wind speed value at location x/y and time from fancy database."""
key = time, location_x, location_y
if key in cache.keys:
idx = cache.keys.index(key)
return cache.values[idx]
# imagine that this is downloaded from some database:
value = np.random.random()
# cache it for next time!
cache.keys.append(key)
cache.values.append(value)
return value
@timer()
def main():
times = np.arange(100)
locations_x = np.arange(15)
locations_y = np.arange(18)
values = [read_wind_speed(t, x, y)
for t, x, y in product(times, locations_x, locations_y)]
print(sum(values) / len(values))
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
main()