From 77bfaaf5a7c4339d5c8c4f93efd19f844ca6258d Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Sun, 16 Apr 2017 00:55:18 -0700 Subject: [PATCH] Further refactor the main harness. Print average time for each solution. --- copycat/copycat.py | 13 +++++++------ copycat/main.py | 4 +++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/copycat/copycat.py b/copycat/copycat.py index 919993d..b0e95ef 100644 --- a/copycat/copycat.py +++ b/copycat/copycat.py @@ -43,10 +43,12 @@ def runTrial(answers): else: answer = None finalTemperature = temperature.value - print 'Answered %s (time %d, final temperature %.1f)' % (answer, coderack.codeletsRun, finalTemperature) - answers[answer] = answers.get(answer, {'count': 0, 'tempsum': 0}) + finalTime = coderack.codeletsRun + print 'Answered %s (time %d, final temperature %.1f)' % (answer, finalTime, finalTemperature) + answers[answer] = answers.get(answer, {'count': 0, 'tempsum': 0, 'timesum': 0}) answers[answer]['count'] += 1 answers[answer]['tempsum'] += finalTemperature + answers[answer]['timesum'] += finalTime def run(initial, modified, target, iterations): @@ -55,7 +57,6 @@ def run(initial, modified, target, iterations): for i in xrange(iterations): runTrial(answers) for answer, d in answers.iteritems(): - d['avgtemp'] = d['tempsum'] / d['count'] - d.pop('tempsum') - for answer, d in sorted(answers.iteritems(), key=lambda kv: kv[1]['avgtemp']): - print '%s: %d (avg temp %.1f)' % (answer, d['count'], d['avgtemp']) + d['avgtemp'] = d.pop('tempsum') / d['count'] + d['avgtime'] = d.pop('timesum') / d['count'] + return answers diff --git a/copycat/main.py b/copycat/main.py index c797067..feb1a68 100644 --- a/copycat/main.py +++ b/copycat/main.py @@ -18,7 +18,9 @@ def main(program, args): else: initial, modified, target = args iterations = 1 - copycat.run(initial, modified, target, iterations) + answers = copycat.run(initial, modified, target, iterations) + for answer, d in sorted(answers.iteritems(), key=lambda kv: kv[1]['avgtemp']): + print '%s: %d (avg time %.1f, avg temp %.1f)' % (answer, d['count'], d['avgtime'], d['avgtemp']) return 0 except ValueError: print >> sys.stderr, 'Usage: %s initial modified target [iterations]' % program