File: timetrial.py

package info (click to toggle)
python-cogent 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,424 kB
  • ctags: 24,343
  • sloc: python: 134,200; makefile: 100; ansic: 17; sh: 10
file content (124 lines) | stat: -rw-r--r-- 3,314 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python

# Simple script to run another command a certain number of times,
# recording how long each run took, and writing the results out to a file.

import os
import os.path
import re
import string
import sys
import time

__author__ = "Peter Maxwell and  Gavin Huttley"
__copyright__ = "Copyright 2007-2012, The Cogent Project"
__credits__ = ["Peter Maxwell", "Gavin Huttley", "Edward Lang"]
__license__ = "GPL"
__version__ = "1.5.3"
__maintainer__ = "Gavin Huttley"
__email__ = "gavin.huttley@anu.edu.au"
__status__ = "Production"

# Values that affect the running of the program.
minimum_accepted_time = 2

iterations = int(sys.argv[1])
args = sys.argv[2:]

script_re = re.compile(".py$")
type = ""

for i in range(len(args)):
    if script_re.search(args[i], 1):
        script = args[i][0:string.index(args[i], '.')]
    if args[i] == "mpirun":
        type = "parallel"

if not type:
    type = "serial"

output = "timing/" + script + "-" + str(int(time.time())) + "-" + type

def usage():
    pass

def standard_dev(numbers = [], mean = 1):
    import math
    
    sum = 0
    
    for i in range(len(numbers)):
        sum = sum + math.pow(numbers[i] - mean, 2)

    sigma = math.sqrt(sum / (len(numbers) - 1))

    return sigma
    
def main():
    if args:
        command = ' '.join(map(str, args))
    else:
        usage()
        sys.exit()

    total_time = 0.0
    times = []

    print 'Running "%s" %d times...' % (command, iterations)
    i = 0
    attempt = 0
    while i < iterations:
        start_time  = time.time()
        os.system(command + " > " + output + "." + str(i))
        end_time    = time.time() - start_time

        if end_time > minimum_accepted_time:
            times.append(end_time)
            total_time  = total_time + end_time
            print "Time for run %d: %.3f seconds" % (i, end_time)
            i = i + 1
            attempt = 0
        else:
            print "Discarding probably bogus time: %.3f seconds" % end_time
            attempt = attempt + 1
        if attempt == 5:
            print "Aborting early due to multiple errors"
            sys.exit(3)
            
    times.sort()
    mean = total_time / len(times)
    sd   = standard_dev(times, mean)
    print ""
    print "Fastest time   : %.3f" % times[0]
    print "Slowest time   : %.3f" % times[len(times) - 1]
    print "Mean           : %.3f" % mean
    print "Standard dev   : %.3f" % sd
    print "Total time     : %.3f" % total_time

    print ""

    corrected_total = 0.0
    corrected_times = []

    for i in range(len(times)):
        if abs(mean - times[i]) < sd:
            corrected_times.append(times[i])
            corrected_total = corrected_total + times[i]
        else:
            print "Discarding value '%.3f'" % times[i]

    if len(times) != len(corrected_times):
        corrected_mean = corrected_total / len(corrected_times)
        corrected_sd   = standard_dev(corrected_times, corrected_mean)
    
        print ""
        print "CORRECTED RESULTS"
        print "Fastest time   : %.3f" % corrected_times[0]
        print "Slowest time   : %.3f" % corrected_times[len(corrected_times)-1]
        print "Mean           : %.3f" % corrected_mean
        print "Standard dev   : %.3f" % corrected_sd
    
if __name__ == "__main__":
    main()