added slow_call vs. fast_call functionality to profile_linker

上级 3d5651e3
...@@ -10,26 +10,46 @@ from copy import copy ...@@ -10,26 +10,46 @@ from copy import copy
class profile_linker: class profile_linker:
def __init__(self, env): def __init__(self, env):
self.order = env.toposort() self.order = env.toposort()
# print "digraph unix { size = '6,6'; node [color = lightblue2; style = filled];"
# for op in self.order:
# for input in op.inputs:
# if input.owner:
# print input.owner.__class__.__name__ + str(abs(id(input.owner))), " -> ", op.__class__.__name__ + str(abs(id(op))), ";"
self.thunks = [op._perform for op in self.order] self.thunks = [op._perform for op in self.order]
self.n_calls = 0 self.n_calls = 0
self.n_thunks = 0
self.times = [0.0 for op in self.order] self.times = [0.0 for op in self.order]
def __call__(self): #TODO: popen2("dot -Tpng | display") and actually make the graph window pop up
def print_for_dot(self):
print "digraph unix { size = '6,6'; node [color = lightblue2; style = filled];"
for op in self.order:
for input in op.inputs:
if input.owner:
print input.owner.__class__.__name__ + str(abs(id(input.owner))), " -> ", op.__class__.__name__ + str(abs(id(op))), ";"
def slow_call(self):
"""Run the program, timing each thunk. """
for i, thunk in enumerate(self.thunks): for i, thunk in enumerate(self.thunks):
start_time = time.time() start_time = time.time()
thunk() thunk()
self.times[i] += time.time() - start_time self.times[i] += time.time() - start_time
self.n_thunks += 1
self.n_calls += 1
def fast_call(self):
"""Run the program, but only time the entire loop."""
start_time = time.time()
for th in self.thunks:
th()
self.n_thunks += len(self.thunks)
self.n_calls += 1 self.n_calls += 1
self.times[0] += time.time() - start_time
__call__ = slow_call
def dump(self): def dump(self, proportion=True):
"""Print statistics accumulated so far."""
total_time = sum(self.times) total_time = sum(self.times)
print self.n_calls, 'calls took', total_time, 'seconds' print self.n_calls, 'calls took', total_time, 'seconds to evaluate',
print self.n_thunks, 'thunks'
if 0:
print 'Proportion of CPU per op' print 'Proportion of CPU per op'
for op, t in zip(self.order, self.times): for op, t in zip(self.order, self.times):
s_op = str(op).split()[0][1:] s_op = str(op).split()[0][1:]
...@@ -41,7 +61,10 @@ class profile_linker: ...@@ -41,7 +61,10 @@ class profile_linker:
s_op = str(op).split()[0][1:] s_op = str(op).split()[0][1:]
dct[s_op] = dct.get(s_op, 0.0) + t dct[s_op] = dct.get(s_op, 0.0) + t
for t, s_op in reversed(sorted([(t,op) for op, t in dct.items()])): for t, s_op in reversed(sorted([(t,op) for op, t in dct.items()])):
if proportion:
print " %-35s %4.5f"% (s_op, t/total_time) print " %-35s %4.5f"% (s_op, t/total_time)
else:
print " %-35s %4.5f"% (s_op, t)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论