Merge pull request #139 from isidroas/main
time.process_time() instead of time.clock()
This commit is contained in:
@@ -214,7 +214,7 @@
|
|||||||
" self.seen = {}\n",
|
" self.seen = {}\n",
|
||||||
" self.diff = 0\n",
|
" self.diff = 0\n",
|
||||||
" self.stack = []\n",
|
" self.stack = []\n",
|
||||||
" self.starttime = time.clock()\n",
|
" self.starttime = time.process_time()\n",
|
||||||
" self.dict = dict\n",
|
" self.dict = dict\n",
|
||||||
" self.steps = 0\n",
|
" self.steps = 0\n",
|
||||||
" for word in L.split(','):\n",
|
" for word in L.split(','):\n",
|
||||||
@@ -286,7 +286,7 @@
|
|||||||
" self.best = len(self)\n",
|
" self.best = len(self)\n",
|
||||||
" self.bestphrase = str(self)\n",
|
" self.bestphrase = str(self)\n",
|
||||||
" print('%5d phrases (%5d words) in %3d seconds (%6d steps)' % (\n",
|
" print('%5d phrases (%5d words) in %3d seconds (%6d steps)' % (\n",
|
||||||
" self.best, self.bestphrase.count(' ')+1, time.clock() - self.starttime,\n",
|
" self.best, self.bestphrase.count(' ')+1, time.process_time() - self.starttime,\n",
|
||||||
" self.steps))\n",
|
" self.steps))\n",
|
||||||
" assert is_unique_palindrome(self.bestphrase)\n",
|
" assert is_unique_palindrome(self.bestphrase)\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -516,9 +516,9 @@
|
|||||||
"def do1(puzzle):\n",
|
"def do1(puzzle):\n",
|
||||||
" \"Do one puzzle; showing puzzle and solution and printing elapsed time.\"\n",
|
" \"Do one puzzle; showing puzzle and solution and printing elapsed time.\"\n",
|
||||||
" show(puzzle)\n",
|
" show(puzzle)\n",
|
||||||
" t0 = time.clock()\n",
|
" t0 = time.process_time()\n",
|
||||||
" solution = solve(Grid(puzzle))\n",
|
" solution = solve(Grid(puzzle))\n",
|
||||||
" t1 = time.clock()\n",
|
" t1 = time.process_time()\n",
|
||||||
" assert is_solution(solution, puzzle)\n",
|
" assert is_solution(solution, puzzle)\n",
|
||||||
" show(solution)\n",
|
" show(solution)\n",
|
||||||
" print('{:.3f} seconds'.format(t1 - t0))\n",
|
" print('{:.3f} seconds'.format(t1 - t0))\n",
|
||||||
@@ -998,9 +998,9 @@
|
|||||||
"def benchmark(label, puzzles=puzzles):\n",
|
"def benchmark(label, puzzles=puzzles):\n",
|
||||||
" \"Run `solve` on these puzzles; record and verify results for this label; print all results.\"\n",
|
" \"Run `solve` on these puzzles; record and verify results for this label; print all results.\"\n",
|
||||||
" n = len(puzzles)\n",
|
" n = len(puzzles)\n",
|
||||||
" t0 = time.clock()\n",
|
" t0 = time.process_time()\n",
|
||||||
" results = [solve(Grid(p)) for p in puzzles]\n",
|
" results = [solve(Grid(p)) for p in puzzles]\n",
|
||||||
" avg = (time.clock() - t0) / len(puzzles)\n",
|
" avg = (time.process_time() - t0) / len(puzzles)\n",
|
||||||
" for (r, p) in zip(results, puzzles):\n",
|
" for (r, p) in zip(results, puzzles):\n",
|
||||||
" assert is_solution(r, p) \n",
|
" assert is_solution(r, p) \n",
|
||||||
" benchmarks[label] = '{:.3f} sec/puzzle ({:.1f} Hz)'.format(avg, 1/avg)\n",
|
" benchmarks[label] = '{:.3f} sec/puzzle ({:.1f} Hz)'.format(avg, 1/avg)\n",
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ class Panama:
|
|||||||
## positive for words on left, negative for right.
|
## positive for words on left, negative for right.
|
||||||
## .stack holds (action, side, arg) tuples
|
## .stack holds (action, side, arg) tuples
|
||||||
update(self, left=[], right=[], best=0, seen={}, diff=0, stack=[],
|
update(self, left=[], right=[], best=0, seen={}, diff=0, stack=[],
|
||||||
used_reversibles=False, starttime=time.clock(), dict=dict)
|
used_reversibles=False, starttime=time.process_time(), dict=dict)
|
||||||
for word in L.split(','):
|
for word in L.split(','):
|
||||||
self.add('left', canonical(word))
|
self.add('left', canonical(word))
|
||||||
for rword in reversestr(R).split(','):
|
for rword in reversestr(R).split(','):
|
||||||
@@ -209,7 +209,7 @@ class Panama:
|
|||||||
self.best = len(self)
|
self.best = len(self)
|
||||||
self.bestphrase = str(self)
|
self.bestphrase = str(self)
|
||||||
print('%5d phrases (%5d words) in %3d seconds' % (
|
print('%5d phrases (%5d words) in %3d seconds' % (
|
||||||
self.best, self.bestphrase.count(' ')+1, time.clock() - self.starttime))
|
self.best, self.bestphrase.count(' ')+1, time.process_time() - self.starttime))
|
||||||
assert is_panama(self.bestphrase)
|
assert is_panama(self.bestphrase)
|
||||||
f = open('pallog%d.txt' % (id(self) % 10000), 'w')
|
f = open('pallog%d.txt' % (id(self) % 10000), 'w')
|
||||||
f.write(self.bestphrase + '\n')
|
f.write(self.bestphrase + '\n')
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ def unit_tests():
|
|||||||
def spelltest(tests, verbose=False):
|
def spelltest(tests, verbose=False):
|
||||||
"Run correction(wrong) on all (right, wrong) pairs; report results."
|
"Run correction(wrong) on all (right, wrong) pairs; report results."
|
||||||
import time
|
import time
|
||||||
start = time.clock()
|
start = time.process_time()
|
||||||
good, unknown = 0, 0
|
good, unknown = 0, 0
|
||||||
n = len(tests)
|
n = len(tests)
|
||||||
for right, wrong in tests:
|
for right, wrong in tests:
|
||||||
@@ -90,7 +90,7 @@ def spelltest(tests, verbose=False):
|
|||||||
if verbose:
|
if verbose:
|
||||||
print('correction({}) => {} ({}); expected {} ({})'
|
print('correction({}) => {} ({}); expected {} ({})'
|
||||||
.format(wrong, w, WORDS[w], right, WORDS[right]))
|
.format(wrong, w, WORDS[w], right, WORDS[right]))
|
||||||
dt = time.clock() - start
|
dt = time.process_time() - start
|
||||||
print('{:.0%} of {} correct ({:.0%} unknown) at {:.0f} words per second '
|
print('{:.0%} of {} correct ({:.0%} unknown) at {:.0f} words per second '
|
||||||
.format(good / n, n, unknown / n, n / dt))
|
.format(good / n, n, unknown / n, n / dt))
|
||||||
|
|
||||||
|
|||||||
@@ -138,9 +138,9 @@ def solve_all(grids, name=''):
|
|||||||
sum(results), N, name, sum(times)/N, N/sum(times), max(times)))
|
sum(results), N, name, sum(times)/N, N/sum(times), max(times)))
|
||||||
|
|
||||||
def time_solve(grid):
|
def time_solve(grid):
|
||||||
start = time.clock()
|
start = time.process_time()
|
||||||
values = solve(grid)
|
values = solve(grid)
|
||||||
t = time.clock()-start
|
t = time.process_time()-start
|
||||||
return (t, solved(values))
|
return (t, solved(values))
|
||||||
|
|
||||||
def solved(values):
|
def solved(values):
|
||||||
|
|||||||
Reference in New Issue
Block a user