Skip to content

Commit e5f9c65

Browse files
committed
tweak from @FeldrinH suggestions
1 parent bf4e9cd commit e5f9c65

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

docs/free-threading_test/sudoku_thread_perf_comparison.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
## values is a dict of possible values, e.g. {'A1':'12349', 'A2':'8', ...}
1313

1414
import os
15+
import sys
16+
try:
17+
_is_gil_enabled = sys._is_gil_enabled()
18+
except:
19+
_is_gil_enabled = True
1520

1621
def cross(A, B):
1722
"Cross product of elements in A and elements in B."
@@ -155,9 +160,9 @@ def solve_all(grids, name='', showif=0.0, nbthreads=1):
155160
When showif is a number of seconds, display puzzles that take longer.
156161
When showif is None, don't display any puzzles."""
157162
def time_solve(grid):
158-
start = time.time()
163+
start = time.perf_counter()
159164
values = solve(grid)
160-
t = time.time()-start
165+
t = time.perf_counter()-start
161166
## Display puzzles that take long enough
162167
if showif is not None and t > showif:
163168
display(grid_values(grid))
@@ -198,12 +203,12 @@ def random_puzzle(N=17):
198203
test()
199204
nbsudoku = 40
200205
thread_list = ( 1, 2, 4, 8, 16)
201-
print(f'there is {os.cpu_count()} logical processors, {os.cpu_count()/2} physical processors')
206+
print(f'there is {os.cpu_count()} logical processors, {os.cpu_count()/2} physical processors, Gil Enabled = {_is_gil_enabled}')
202207
reference_delta = 0
203208
for nbthreads in thread_list:
204-
startall = time.time()
209+
startall = time.perf_counter()
205210
solve_all([hard2]*nbsudoku, "hard2", None, nbthreads=nbthreads)
206-
new_delta = time.time()-startall
211+
new_delta = time.perf_counter()-startall
207212
if reference_delta ==0 :
208213
reference_delta = new_delta
209214
ratio = reference_delta/(new_delta)

docs/free-threading_test/sudoku_thread_perf_comparison_typed.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
import re
1919
import time, random
2020
from concurrent.futures import ThreadPoolExecutor
21+
import sys
22+
try:
23+
_is_gil_enabled = sys._is_gil_enabled()
24+
except:
25+
_is_gil_enabled = True
2126

2227
DigitSet = str # e.g. '123'
2328
Square = str # e.g. 'A9'
@@ -141,9 +146,9 @@ def solve_all(puzzles, name, showif=0.0, nbthreads=1):
141146
When showif is a number of seconds, display puzzles that take longer.
142147
When showif is None, don't display any puzzles."""
143148
def time_solve(puzzle):
144-
start = time.time()
149+
start = time.perf_counter()
145150
solution = search(constrain(puzzle))
146-
t = time.time()-start
151+
t = time.perf_counter()-start
147152
## Display puzzles that take long enough
148153
if showif is not None and t > showif and is_solution(solution, puzzle):
149154
print('\nPuzzle ', sep, 'Solution')
@@ -215,16 +220,16 @@ def random_puzzle(N=17):
215220
#print(picture(grid1))
216221
nbsudoku = 40
217222
thread_list = ( 1, 2, 4, 8, 16)
218-
print(f'there is {os.cpu_count()} logical processors, {os.cpu_count()/2} physical processors')
223+
print(f'there is {os.cpu_count()} logical processors, {os.cpu_count()/2} physical processors, Gil Enabled = {_is_gil_enabled}')
219224
reference_delta = 0
220225
my_puzzles = parse_grids([hard2]*nbsudoku)
221226
for nbthreads in thread_list:
222-
startall = time.time()
227+
startall = time.perf_counter()
223228
#hardest = parse_grids(open('hardest.txt'))
224229
#grids10k = parse_grids(open('sudoku10k.txt'))
225230
#solve(puzzles=my_puzzles, verbose=False)
226231
solve_all(puzzles=my_puzzles, name='hard2', showif=None, nbthreads=nbthreads)
227-
new_delta = time.time()-startall
232+
new_delta = time.perf_counter()-startall
228233
if reference_delta ==0 :
229234
reference_delta = new_delta
230235
ratio = reference_delta/(new_delta)

docs/free-threading_test/thread1.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22
import time, random
33
from concurrent.futures import ThreadPoolExecutor
44

5-
print(f"nogil={getattr(sys.flags, 'nogil', False)}")
5+
try:
6+
_is_gil_enabled = sys._is_gil_enabled()
7+
except:
8+
_is_gil_enabled = True
9+
10+
print(f"Gil Enabled = {_is_gil_enabled}")
611

712
def fib(n):
813
if n < 2: return 1
914
return fib(n-1) + fib(n-2)
1015

1116
threads = 1
12-
start = time.time()
17+
start = time.perf_counter()
1318
if len(sys.argv) > 1:
1419
threads = int(sys.argv[1])
1520

1621
with ThreadPoolExecutor(max_workers=threads) as executor:
1722
for _ in range(threads):
1823
executor.submit(lambda: print(fib(34)))
19-
t = time.time()-start
24+
t = time.perf_counter()-start
2025
print("Solved g %.2f secs " %t)

docs/free-threading_test/thread20.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22
import time, random
33
from concurrent.futures import ThreadPoolExecutor
44

5-
print(f"nogil={getattr(sys.flags, 'nogil', False)}")
5+
try:
6+
_is_gil_enabled = sys._is_gil_enabled()
7+
except:
8+
_is_gil_enabled = True
9+
10+
print(f"Gil Enabled = {_is_gil_enabled}")
611

712
def fib(n):
813
if n < 2: return 1
914
return fib(n-1) + fib(n-2)
1015

1116
threads = 20
12-
start = time.time()
17+
start = time.perf_counter()
1318
if len(sys.argv) > 1:
1419
threads = int(sys.argv[1])
1520

1621
with ThreadPoolExecutor(max_workers=threads) as executor:
1722
for _ in range(threads):
1823
executor.submit(lambda: print(fib(34)))
19-
t = time.time()-start
24+
t = time.perf_counter()-start
2025
print("Solved g %.2f secs " %t)

docs/free-threading_test/thread4.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22
import time, random
33
from concurrent.futures import ThreadPoolExecutor
44

5-
print(f"nogil={getattr(sys.flags, 'nogil', False)}")
5+
try:
6+
_is_gil_enabled = sys._is_gil_enabled()
7+
except:
8+
_is_gil_enabled = True
9+
10+
print(f"Gil Enabled = {_is_gil_enabled}")
611

712
def fib(n):
813
if n < 2: return 1
914
return fib(n-1) + fib(n-2)
1015

1116
threads = 4
12-
start = time.time()
17+
start = time.perf_counter()
1318
if len(sys.argv) > 1:
1419
threads = int(sys.argv[1])
1520

1621
with ThreadPoolExecutor(max_workers=threads) as executor:
1722
for _ in range(threads):
1823
executor.submit(lambda: print(fib(34)))
19-
t = time.time()-start
24+
t = time.perf_counter()-start
2025
print("Solved g %.2f secs " %t)

0 commit comments

Comments
 (0)