usr bin env python from __future__ import with_statement import sys im

 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
#!/usr/bin/env python
from __future__ import with_statement
import sys
import re
# Format:
# 0: call count
# 1: recursivity flag
# 2: total time
# 3: inline time
FUN_RE = re.compile(r'^([^:]+):\d+\((\w+)\)$')
def name_strip(fun):
fun = fun.strip('+')
m = FUN_RE.search(fun)
if m:
return '%s(%s)' % m.groups()
return fun
def read_profile(fname):
info = file(fname).readlines()
result = {}
for line in info[1:]:
data = line.split()
result[name_strip(' '.join(data[4:]))] = (
int(data[0].strip('+')),
float(data[2]),
float(data[3]))
return result
def merge(p1, p2):
keys = set(p1.keys())
keys = keys.union(p2.keys())
for k in keys:
yield (k, p1.get(k), p2.get(k))
def negative(line):
if not line:
return None
return (line[0], -line[1], -line[2])
def diff_profile(prof_union):
for key, l1, l2 in prof_union:
if not l1 or not l2:
yield ((l1 and "<" or ">") + key, ) + (negative(l1) or l2)
else:
in_diff = l2[2] - l1[2]
tot_diff = l2[1] - l1[1]
call_count = (l1[0] != l2[0]) and ('%s -> %s' % (l1[0], l2[0])) or l1[0]
yield (" " + key, call_count, tot_diff, in_diff)
def print_formatted(diff):
format = "%(key)s\t%(total)s\t\t%(inline)s\t\t%(call)s"
def pad(s, l):
return (s + " " * l)[:l]
length = max(len(i[0]) for i in diff)
for key, call, total, inline in diff:
key = pad(key, length)
print format % locals()
def compare(fname1, fname2):
p1 = read_profile(fname1)
p2 = read_profile(fname2)
diff = diff_profile(merge(p1, p2))
diff = sorted(diff, key=lambda x: x[2], reverse=True)
print_formatted(diff)
if __name__ == '__main__':
try:
p1, p2 = sys.argv[1:]
except ValueError:
p1, p2 = 'p1', 'p2'
compare(p1, p2)