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
|
"""Shared routines for the plotters."""
import fileinput
import collections
Record = collections.namedtuple('Record', 'variant function bytes loops alignment elapsed rest')
def parse_value(v):
"""Turn text into a primitive"""
try:
if '.' in v:
return float(v)
else:
return int(v)
except ValueError:
return v
def unique(records, name, prefer=''):
"""Return the unique values of a column in the records"""
values = list(set(getattr(x, name) for x in records))
if not values:
return values
elif type(values[0]) == str:
return sorted(values, key=lambda x: '%-06d|%s' % (-prefer.find(x), x))
else:
return sorted(values)
def parse():
"""Parse a record file into named tuples, correcting for loop
overhead along the way.
"""
records = [Record(*[parse_value(y) for y in x.split(':')]) for x in fileinput.input()]
# Pull out any bounce values
costs = {}
for record in [x for x in records if x.function=='bounce']:
costs[(record.bytes, record.loops)] = record.elapsed
# Fix up all of the records for cost
out = []
for record in records:
if record.function == 'bounce':
continue
cost = costs.get((record.bytes, record.loops), None)
if not cost:
out.append(record)
else:
# Unfortunately you can't update a namedtuple...
values = list(record)
values[-2] -= cost
out.append(Record(*values))
return out
|