Commit | Line | Data |
---|---|---|
c2df194c MW |
1 | #! /usr/bin/python |
2 | ||
3 | import sre as R | |
4 | import os as OS | |
5 | from popen2 import Popen3 | |
6 | from sys import argv | |
7 | import catacomb as C | |
8 | ||
9 | r_score = R.compile(r'^Solved in ([\d]+) guesses$') | |
10 | r_time = R.compile(r'^time:([\d.]+),([\d.]+)$') | |
11 | def run(prog): | |
12 | proc = Popen3(['time', '-f', 'time:%U,%S'] + prog, capturestderr = True) | |
13 | guesses = -1 | |
14 | time = -1 | |
15 | proc.tochild.close() | |
16 | for l in proc.fromchild: | |
17 | m = r_score.match(l) | |
18 | if m: guesses = int(m.group(1)) | |
19 | for l in proc.childerr: | |
20 | m = r_time.match(l) | |
21 | if m: | |
22 | u, s = map(float, m.groups([1, 2])) | |
23 | time = u + s | |
24 | return guesses, time | |
25 | ||
26 | impl = argv[1:] | |
27 | def compare(h, c, scores): | |
28 | code = [C.rand.range(c) for i in xrange(h)] | |
29 | perf = {} | |
30 | best, bestperf = [None, None], [-1, -1] | |
31 | for i in impl: | |
32 | p = perf[i] = run(i + map(str, [h, c] + code)) | |
33 | for j in [0, 1]: | |
34 | if bestperf[j] == -1 or p[j] < bestperf[j]: | |
35 | bestperf[j] = p[j] | |
36 | best[j] = i | |
37 | scores[i][j] += p[j] | |
38 | st = '' | |
39 | for i in [0, 1]: | |
40 | st += '%s:' % ['G', 'T'][i] | |
41 | for j in impl: | |
42 | if best[i] == j: | |
43 | st += '+' | |
44 | else: | |
45 | st += '.' | |
46 | st += ' ' | |
47 | print '%s%s' % (st, code) | |
48 | ind = (len(impl) + 3) * len(best) | |
49 | for i in impl: | |
50 | ##print '%s%s: %s' % (' ' * ind, i, ' '.join(map(str, perf[i]))) | |
51 | pass | |
52 | ||
53 | ||
54 | for n, h, c in [(100, 4, 6), | |
55 | (50, 4, 10), | |
56 | (20, 5, 6), | |
57 | (20, 5, 8), | |
58 | (20, 5, 10)]: | |
59 | print '************************** %d %d' % (h, c) | |
60 | scores = {} | |
61 | for i in impl: scores[i] = [0, 0] | |
62 | for i in xrange(n): | |
63 | compare(h, c, scores) | |
64 | print 'Summary, %d %d' % (h, c) | |
65 | for i in impl: | |
66 | print ' %s: %s' % (i, ' '.join(['%s:%s' % (['G', 'T'][j], scores[i][j]) | |
67 | for j in [0, 1]])) | |
68 |