-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_benchmarks.py
58 lines (46 loc) · 1.58 KB
/
run_benchmarks.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
# Benchmark for read of raster data to ndarray
import timeit
import math
import os
import os.path
import gc
import json
IMPLEMENTATION = 'rasterio' # arcpy
def floor(n):
return int(math.floor(n))
def mean(dataset):
return sum(dataset) / len(dataset)
def median(dataset):
return quantile(dataset, 2)
def quantile(dataset, partition, partitions=4):
return dataset[floor(partition * len(dataset) / partitions)]
def summary_statistics(dataset):
return {
'min': min(dataset),
'mean': mean(dataset),
'median': median(dataset),
'max': max(dataset),
'first_quartile': quantile(dataset, 1),
'third_quartile': quantile(dataset, 3),
'dataset': dataset
}
def microseconds(t):
return t * 1000
def profile(stmt='pass', setup='pass', run_gc=False):
n = 40
setup = '\ndef profile(f):\n return f\n\ngc.collect();' + setup
if run_gc:
setup = 'gc.enable();' + setup
t = timeit.Timer(stmt, setup)
results = [microseconds(x / n) for x in t.repeat(repeat=n, number=n)]
return summary_statistics(results)
results = {IMPLEMENTATION: {}}
benchmarks_dir = os.path.abspath(os.path.join("scripts", IMPLEMENTATION, "benchmarks"))
for filename in os.listdir(benchmarks_dir):
with open(os.path.join(benchmarks_dir, filename)) as f:
benchmark = f.read()
setup, stmt = benchmark.split('# ---')
# print 'Benchmarking:', filename
results[IMPLEMENTATION][filename] = profile(stmt, setup)
results[IMPLEMENTATION][filename + 'withGCon'] = profile(stmt, setup, True)
print json.dumps(results)