Commits

Anonymous committed e032963

collecting total_time in LineProfiler, fixed percentage of function calls

Comments (0)

Files changed (2)

_line_profiler.pyx

         self.code_map = {}
         self.last_time = {}
         self.timer_unit = hpTimerUnit()
+        self.total_time = 0
+        self.last_start = 0
         self.enable_count = 0
         for func in functions:
             self.add_function(func)
 
     def enable(self):
         PyEval_SetTrace(python_trace_callback, self)
+        self.last_start = hpTimer()
 
     def disable(self):
+        current_tick = hpTimer()
         self.last_time = {}
         unset_trace()
 
+        self.total_time += (current_tick - self.last_start)
+        self.last_start = 0
+
     def get_stats(self):
         """ Return a LineStats object containing the timings.
         """
         finally:
             f.close()
 
-    def print_stats(self, stream=None):
+    def print_stats(self, combined = True, stream=None):
         """ Show the gathered statistics.
         """
         lstats = self.get_stats()
-        show_text(lstats.timings, lstats.unit, stream=stream)
+        if combined:
+            # show stats combined for all functions of the same file
+            show_all(lstats.timings, lstats.unit, self.total_time, stream = stream)
+        else:
+            # show separate stats for each function
+            show_text(lstats.timings, lstats.unit, stream=stream)
 
     def reset_stats(self):
-        for v in self.code_map.values():
-            v.clear()
+        self.code_map.clear()
 
     def run(self, cmd):
         """ Profile a single executable statment in the main namespace.
     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):
+def show_all(stats, unit, total_time, 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
+    # collect function timings
     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
+        func_times[(fn, start_lineno)] = func_time
 
     empty = ('', '', '', '')
     template = '%6s %9s %12s %12s %8s  %-s'
 
         d = {}
         for lineno, nhits, time in timings:
-            d[lineno] = (nhits, "%11.6f" % (time * unit), '%5.1f' % (float(time) * unit/ nhits),
-                '%5.1f' % (100*time / total_time))
+            time = float(time)
+            d[lineno] = (nhits, "%11.6f" % (time * unit), '%9.6f' % (float(time) * unit/ nhits), '%5.2f' % (100*time / total_time))
 
         all_lines = linecache.getlines(fn)
         sublines = inspect.getblock(all_lines[start_lineno-1:])
             dd[lineno] = (d.get(lineno, empty), line.rstrip("\n").rstrip("\r"))
 
         func_time = func_times[(fn, start_lineno)]
-        dd[start_lineno] = (("", "%11.6f" % func_time, "", "%5.1f" % (100*func_time / total_time)), sublines[0].rstrip('\n').rstrip('\r'))
+        dd[start_lineno] = (("", "%11.6f" % (func_time * unit), "", "%5.2f" % (100*func_time / total_time)), sublines[0].rstrip('\n').rstrip('\r'))
 
 
     # dump all lines
         for lineno, line_data in sorted(dd.items()):
             if last_lineno is not None and last_lineno != lineno - 1:
                 print >> stream, ""
+                print >> stream, header
             last_lineno = lineno
 
             (nhits, time, per_hit, percent), line = line_data