Commits

Anonymous committed 0c55b8f

show_all() to show all profiled lines file-wise

Comments (0)

Files changed (1)

     for (fn, lineno, name), timings in sorted(stats.items()):
         show_func(fn, lineno, name, stats[fn, lineno, name], unit, stream=stream)
 
+def show_all(stats, unit, stream = None):
+    if stream is None:
+        stream = sys.stdout
+    print >> stream, 'Timer unit: %g s' % unit
+
+    total_time = 0.0
+
+    from collections import defaultdict
+    fn_map = defaultdict(dict)
+    func_times = {}
+
+    # collect total_time
+    for (fn, start_lineno, name), timings in stats.items():
+        if not os.path.exists(fn):
+            continue
+        func_time = 0.0
+        for lineno, nhits, time in timings:
+            func_time += time
+        func_times[(fn, start_lineno)] = func_time * unit
+        total_time += func_time
+
+    empty = ('', '', '', '')
+    template = '%6s %9s %12s %12s %8s  %-s'
+    header = template % ('Line #', 'Hits', 'Time', 'Per Hit', '% Time', 'Line Contents')
+
+    # collect line information
+    for (fn, start_lineno, name), timings in stats.items():
+        if not os.path.exists(fn):
+            continue
+
+        d = {}
+        for lineno, nhits, time in timings:
+            d[lineno] = (nhits, time * unit, '%5.1f' % (float(time) * unit/ nhits),
+                '%5.1f' % (100*time / total_time))
+
+        all_lines = linecache.getlines(fn)
+        sublines = inspect.getblock(all_lines[start_lineno-1:])
+        linenos = range(start_lineno, start_lineno + len(sublines))
+
+        dd = fn_map[fn]
+        for lineno, line in zip(linenos, sublines):
+            if dd.get(lineno, (empty, None))[0] != empty:
+                continue
+            dd[lineno] = (d.get(lineno, empty), line.rstrip("\n").rstrip("\r"))
+
+        func_time = func_times[(fn, start_lineno)]
+        dd[start_lineno] = (("", func_time, "", "%5.1f" % (100*func_time / total_time)), sublines[0].rstrip('\n').rstrip('\r'))
+
+
+    # dump all lines
+    print >> stream, "Total time: %g s" % (total_time * unit)
+    print >> stream, ''
+
+    for fn, dd in sorted(fn_map.items()):
+        print >> stream, ""
+        print >> stream, fn
+        print >> stream, header
+        print >> stream, '=' * len(header)
+
+        last_lineno = None
+        for lineno, line_data in sorted(dd.items()):
+            if last_lineno is not None and last_lineno != lineno - 1:
+                print >> stream, ""
+            last_lineno = lineno
+
+            (nhits, time, per_hit, percent), line = line_data
+            line = template % (lineno, nhits, time, per_hit, percent, line)
+            print >> stream, line
+
+    print >>stream, ""
+
+
 # A %lprun magic for IPython.
 def magic_lprun(self, parameter_s=''):
     """ Execute a statement under the line-by-line profiler from the