3 """Measure how a sorting executable scales with N.
5 The executable should support one of the following:
6 executable path/to/data/file
7 cat path/to/data/file | executable
8 Where the data file is of the format output by data.py.
17 matplotlib.use('Agg') # select backend that doesn't require X Windows
21 def generate_data(stream, N, ordered=False):
22 print >> sys.stderr, 'generate %d data points (ordered? %s)' % (
26 args = ['./data.py', str(N)]
28 args.insert(1, '--ordered')
29 q = subprocess.Popen(args, stdout=stream)
31 assert status == 0, status
34 def run_test(executable, stdin=True, data_filename=None):
35 print >> sys.stderr, 'run %s' % executable
37 with open(data_filename, 'r') as f:
41 p = subprocess.Popen([executable],
42 stdout=open('/dev/null', 'w'))
43 p.communicate(contents)
45 p = subprocess.Popen([executable, data_filename],
46 stdout=open('/dev/null', 'w'))
49 assert status == 0, status
52 def run_tests(executable, stdin=True, data_file=None, ordered=False,
53 repeats=10, max_time=1e2):
57 while prev_time < max_time:
59 ts = numpy.zeros((repeats,), dtype=numpy.double)
60 for i in range(repeats):
61 generate_data(data_file, N, ordered=ordered)
62 ts[i] = run_test(executable, stdin, data_file.name)
69 if __name__ == '__main__':
73 p = optparse.OptionParser(
74 usage='%prog [options] executable', epilog=__doc__)
75 p.add_option('-s', '--stdin', dest='stdin', default=False,
76 action='store_true', help='Use the stdin executable syntax.')
77 p.add_option('-r', '--repeats', dest='repeats', default=10, type='int',
78 help='Number of repeats to run at each N (%default).')
79 p.add_option('-m', '--max-time', dest='max_time', default=1e2,type='float',
80 help='Number of repeats to run at each N (%default).')
81 p.add_option('-p', '--plot', dest='plot', default=None,
82 help='Filename for a scaling plot (no plot is generated if this option is not set).')
84 options,args = p.parse_args()
88 data_file = tempfile.NamedTemporaryFile()
90 'executable': executable,
91 'stdin': options.stdin,
92 'data_file': data_file,
93 'repeats': options.repeats,
94 'max_time': options.max_time,
97 times = run_tests(ordered=False, **kwargs)
98 ordered_times = run_tests(ordered=True, **kwargs)
104 'ordered mean (s)', 'ordered std. dev. (s)',
105 'random mean (s)', 'random std. dev. (s)']
106 plots = dict([(c, []) for c in columns])
108 print '# sort times for %s' % executable
109 print '# %d repeats' % options.repeats
110 print '#%s' % '\t'.join(columns)
111 invalid = numpy.array(numpy.inf)
112 for key in sorted(set(times.keys() + ordered_times.keys())):
113 om = ordered_times.get(key, invalid).mean()
114 os = ordered_times.get(key, invalid).std()
115 m = times.get(key, invalid).mean()
116 s = times.get(key, invalid).std()
117 print '\t'.join([str(x) for x in [key, om, os, m, s]])
118 for c,x in zip(columns, [key, om, os, m, s]):
125 for c,color in zip(['ordered', 'random'], 'br'):
128 y=plots['%s mean (s)' % c],
129 yerr=plots['%s std. dev. (s)' % c],
130 fmt='%so-' % color, label=c)
131 a.set_title('sort times for %s' % executable)
135 a.set_ylabel('t (s)')
137 f.savefig(options.plot)