Merge pull request #139 from isidroas/main

time.process_time() instead of time.clock()
This commit is contained in:
Peter Norvig 2025-05-30 14:09:04 -07:00 committed by GitHub
commit 73261b0e0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 12 additions and 12 deletions

View File

@ -214,7 +214,7 @@
" self.seen = {}\n",
" self.diff = 0\n",
" self.stack = []\n",
" self.starttime = time.clock()\n",
" self.starttime = time.process_time()\n",
" self.dict = dict\n",
" self.steps = 0\n",
" for word in L.split(','):\n",
@ -286,7 +286,7 @@
" self.best = len(self)\n",
" self.bestphrase = str(self)\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",
" assert is_unique_palindrome(self.bestphrase)\n",
"\n",

View File

@ -516,9 +516,9 @@
"def do1(puzzle):\n",
" \"Do one puzzle; showing puzzle and solution and printing elapsed time.\"\n",
" show(puzzle)\n",
" t0 = time.clock()\n",
" t0 = time.process_time()\n",
" solution = solve(Grid(puzzle))\n",
" t1 = time.clock()\n",
" t1 = time.process_time()\n",
" assert is_solution(solution, puzzle)\n",
" show(solution)\n",
" print('{:.3f} seconds'.format(t1 - t0))\n",
@ -998,9 +998,9 @@
"def benchmark(label, puzzles=puzzles):\n",
" \"Run `solve` on these puzzles; record and verify results for this label; print all results.\"\n",
" n = len(puzzles)\n",
" t0 = time.clock()\n",
" t0 = time.process_time()\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",
" assert is_solution(r, p) \n",
" benchmarks[label] = '{:.3f} sec/puzzle ({:.1f} Hz)'.format(avg, 1/avg)\n",

View File

@ -127,7 +127,7 @@ class Panama:
## positive for words on left, negative for right.
## .stack holds (action, side, arg) tuples
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(','):
self.add('left', canonical(word))
for rword in reversestr(R).split(','):
@ -209,7 +209,7 @@ class Panama:
self.best = len(self)
self.bestphrase = str(self)
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)
f = open('pallog%d.txt' % (id(self) % 10000), 'w')
f.write(self.bestphrase + '\n')

View File

@ -79,7 +79,7 @@ def unit_tests():
def spelltest(tests, verbose=False):
"Run correction(wrong) on all (right, wrong) pairs; report results."
import time
start = time.clock()
start = time.process_time()
good, unknown = 0, 0
n = len(tests)
for right, wrong in tests:
@ -90,7 +90,7 @@ def spelltest(tests, verbose=False):
if verbose:
print('correction({}) => {} ({}); expected {} ({})'
.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 '
.format(good / n, n, unknown / n, n / dt))

View File

@ -138,9 +138,9 @@ def solve_all(grids, name=''):
sum(results), N, name, sum(times)/N, N/sum(times), max(times)))
def time_solve(grid):
start = time.clock()
start = time.process_time()
values = solve(grid)
t = time.clock()-start
t = time.process_time()-start
return (t, solved(values))
def solved(values):