Skip to content

Commit 056395a

Browse files
committed
Final code for Cython threading.
1 parent 717312e commit 056395a

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/11-cython/perf/compute_cython.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ def main():
99

1010
t0 = datetime.datetime.now()
1111

12-
# do_math(num=30000000)
12+
# math_core.do_math(num=30_000_000)
1313
print("Doing math on {:,} processors.".format(multiprocessing.cpu_count()))
1414

1515
processor_count = multiprocessing.cpu_count()
1616
threads = []
1717
for n in range(1, processor_count + 1):
1818
threads.append(Thread(target=math_core.do_math,
19-
args=(30_000_000 * (n - 1) / processor_count,
20-
30_000_000 * n / processor_count),
19+
args=(300_000 * (n - 1) / processor_count,
20+
300_000 * n / processor_count),
2121
daemon=True)
2222
)
2323

@@ -27,7 +27,7 @@ def main():
2727
dt = datetime.datetime.now() - t0
2828
print("Done in {:,.2f} sec. (factor: {:,.2f}x)".format(
2929
dt.total_seconds(),
30-
8.54/dt.total_seconds())
30+
.09/dt.total_seconds())
3131
)
3232

3333

src/11-cython/perf/compute_it.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def main():
77

88
t0 = datetime.datetime.now()
99

10-
do_math(num=30_000_000)
10+
do_math(num=300_000)
1111

1212
dt = datetime.datetime.now() - t0
1313
print("Done in {:,.2f} sec.".format(dt.total_seconds()))

src/11-cython/perf/math_core.pyx

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import math
1+
from libc.math cimport sqrt
22

3-
def do_math(start=0, num=10):
4-
pos = start
5-
k_sq = 1000 * 1000
6-
while pos < num:
7-
pos += 1
8-
math.sqrt((pos - k_sq) * (pos - k_sq))
3+
import cython
4+
5+
def do_math(start: cython.int = 0, num: cython.int = 10):
6+
pos: cython.int = start
7+
k_sq: cython.int = 1000 * 1000
8+
9+
with nogil:
10+
while pos < num:
11+
pos += 1
12+
sqrt((pos - k_sq) * (pos - k_sq))

0 commit comments

Comments
 (0)