#!/usr/bin/python

# For use with Scoped_Stopwatch result files

import sys

def main(args):
    file_names = [name for name in args if not name.startswith('-')]
    time_unit = 'sec'
    if len(file_names) < len(args):
        time_unit = [unit for unit in args if unit not in file_names][0][1:]
    timings = dict()
    for index, file_name in enumerate(file_names, 1):
        with open(file_name) as f:
            print('Reading file ' + str(index) + ' of ' + str(len(file_names)) + ': ' + file_name, flush=True, file=sys.stderr)
            for line_number, line in enumerate(f):
                try:
                    name, time = line.split('|')
                except ValueError as e:
                    print('ERROR:', line_number, line)
                    print(e)
                    return
                try:
                    count, time_sum = timings[name]
                except KeyError:
                    count = 0
                    time_sum = 0
                timings[name] = (count + 1, time_sum + float(time))
    
    if time_unit == 'sec':
        multiplier = 1
    elif time_unit == 'msec':
        multiplier = 1e3
    elif time_unit == 'usec':
        multiplier = 1e6
    elif time_unit == 'nsec':
        multiplier = 1e9
    else:
        print('Invalid time unit:', time_unit)
        sys.exit(1)

    print('Average time (' + time_unit + ')|Total time (sec)|Count|Name') # Column Headings
    for name in timings:
        count, time_sum = timings[name]
        print(str(multiplier*time_sum/count) + '|' + str(time_sum) + '|' + str(count) + '|' + name)

if __name__ == "__main__":
    main(sys.argv[1:])

@@ -1402,16 +1402,29 @@
 )%7CCount%7C
+Percent Time%7C
 Name') #
@@ -1440,16 +1440,69 @@
 eadings%0A
+    total_time = sum(x%5B1%5D for x in timings.values())%0A
     for 
@@ -1606,24 +1606,38 @@
 unt) + '%7C' +
+%0A             
  str(time_su
@@ -1646,16 +1646,30 @@
  + '%7C' +
+%0A             
  str(cou
@@ -1679,16 +1679,81 @@
  + '%7C' +
+%0A              str(100*time_sum/total_time) + '%7C' +%0A             
  name)%0A%0A

Add column for percent time with stopwatch results