Skip to content

Commit 717312e

Browse files
committed
Cython but still GIL bound threaded example.
1 parent 1ce7779 commit 717312e

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,4 @@ src/11-cython/.idea/misc.xml
139139
src/11-cython/.idea/modules.xml
140140
src/11-cython/.idea/workspace.xml
141141
greeter.c
142+
math_core.c

Diff for: src/11-cython/perf/compute_cython.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import datetime
2-
import math
2+
import math_core
33
from threading import Thread
44
import multiprocessing
55

66

77
def main():
8-
do_math(1)
8+
math_core.do_math(1)
99

1010
t0 = datetime.datetime.now()
1111

@@ -15,7 +15,7 @@ def main():
1515
processor_count = multiprocessing.cpu_count()
1616
threads = []
1717
for n in range(1, processor_count + 1):
18-
threads.append(Thread(target=do_math,
18+
threads.append(Thread(target=math_core.do_math,
1919
args=(30_000_000 * (n - 1) / processor_count,
2020
30_000_000 * n / processor_count),
2121
daemon=True)
@@ -31,12 +31,12 @@ def main():
3131
)
3232

3333

34-
def do_math(start=0, num=10):
35-
pos = start
36-
k_sq = 1000 * 1000
37-
while pos < num:
38-
pos += 1
39-
math.sqrt((pos - k_sq) * (pos - k_sq))
34+
# def do_math(start=0, num=10):
35+
# pos = start
36+
# k_sq = 1000 * 1000
37+
# while pos < num:
38+
# pos += 1
39+
# math.sqrt((pos - k_sq) * (pos - k_sq))
4040

4141

4242
if __name__ == '__main__':

Diff for: 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=30000000)
10+
do_math(num=30_000_000)
1111

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

Diff for: src/11-cython/perf/math_core.pyx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import math
2+
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))

Diff for: src/11-cython/perf/setup.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from distutils.core import setup
2+
from Cython.Build import cythonize
3+
4+
setup(
5+
ext_modules=cythonize("math_core.pyx")
6+
)

0 commit comments

Comments
 (0)