Skip to content

Commit 23d7f68

Browse files
committed
2 parents 7cc74ee + b1e70ce commit 23d7f68

13 files changed

+212
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,9 @@ src/10-async-web/.idea/webResources.xml
134134
src/10-async-web/.idea/workspace.xml
135135
src/10-async-web/.idea/dictionaries/mkennedy.xml
136136
Project_Default.xml
137+
src/11-cython/.idea/11-cython.iml
138+
src/11-cython/.idea/misc.xml
139+
src/11-cython/.idea/modules.xml
140+
src/11-cython/.idea/workspace.xml
141+
greeter.c
142+
math_core.c

src/11-cython/.idea/dictionaries/screencaster.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/11-cython/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/11-cython/hello_world/greeter.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def greet(name: str):
2+
print("Hello {}".format(name))
3+
print(" ** Running from {} **".format(__file__))

src/11-cython/hello_world/program.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import greeter
2+
3+
4+
def main():
5+
name = input("What is your name? ")
6+
greeter.greet(name)
7+
8+
9+
if __name__ == '__main__':
10+
main()

src/11-cython/hello_world/setup.py

Lines changed: 6 additions & 0 deletions
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("greeter.pyx")
6+
)

src/11-cython/perf/compute_cython.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import datetime
2+
import math_core
3+
from threading import Thread
4+
import multiprocessing
5+
6+
7+
def main():
8+
math_core.do_math(1)
9+
10+
print("Doing math on {:,} processors.".format(multiprocessing.cpu_count()))
11+
12+
processor_count = multiprocessing.cpu_count()
13+
threads = []
14+
for n in range(1, processor_count + 1):
15+
threads.append(Thread(target=math_core.do_math,
16+
args=(3_000_000 * (n - 1) / processor_count,
17+
3_000_000 * n / processor_count),
18+
daemon=True)
19+
)
20+
21+
t0 = datetime.datetime.now()
22+
[t.start() for t in threads]
23+
24+
[t.join() for t in threads]
25+
# math_core.do_math(num=300_000)
26+
27+
dt = datetime.datetime.now() - t0
28+
print("Done in {:,.2f} sec. (factor: {:,.2f}x)".format(
29+
dt.total_seconds(),
30+
0.80 / dt.total_seconds())
31+
)
32+
33+
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))
40+
41+
42+
if __name__ == '__main__':
43+
main()

src/11-cython/perf/compute_it.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import datetime
2+
import math
3+
4+
5+
def main():
6+
do_math(1)
7+
8+
t0 = datetime.datetime.now()
9+
10+
do_math(num=3_000_000)
11+
12+
dt = datetime.datetime.now() - t0
13+
print("Done in {:,.2f} sec.".format(dt.total_seconds()))
14+
15+
16+
def do_math(start=0, num=10):
17+
pos = start
18+
k_sq = 1000 * 1000
19+
while pos < num:
20+
pos += 1
21+
dist = math.sqrt((pos - k_sq)*(pos - k_sq))
22+
23+
24+
if __name__ == '__main__':
25+
main()
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import datetime
2+
import math
3+
import multiprocessing
4+
5+
6+
def main():
7+
do_math(1)
8+
9+
t0 = datetime.datetime.now()
10+
11+
print("Doing math on {:,} processors.".format(multiprocessing.cpu_count()))
12+
13+
pool = multiprocessing.Pool()
14+
processor_count = multiprocessing.cpu_count()
15+
tasks = []
16+
for n in range(1, processor_count + 1):
17+
task = pool.apply_async(do_math, (30_000_000 * (n - 1) / processor_count,
18+
30_000_000 * n / processor_count))
19+
tasks.append(task)
20+
21+
pool.close()
22+
pool.join()
23+
24+
dt = datetime.datetime.now() - t0
25+
print("Done in {:,.2f} sec. (factor: {:,.2f}x)".format(
26+
dt.total_seconds(),
27+
8.54 / dt.total_seconds())
28+
)
29+
30+
31+
def do_math(start=0, num=10):
32+
pos = start
33+
k_sq = 1000 * 1000
34+
ave = 0
35+
while pos < num:
36+
pos += 1
37+
val = math.sqrt((pos - k_sq) * (pos - k_sq))
38+
ave += val / num
39+
40+
return int(ave)
41+
42+
43+
if __name__ == '__main__':
44+
main()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import datetime
2+
import math
3+
from threading import Thread
4+
import multiprocessing
5+
6+
7+
def main():
8+
do_math(1)
9+
10+
t0 = datetime.datetime.now()
11+
12+
# do_math(num=30000000)
13+
print("Doing math on {:,} processors.".format(multiprocessing.cpu_count()))
14+
15+
processor_count = multiprocessing.cpu_count()
16+
threads = []
17+
for n in range(1, processor_count + 1):
18+
threads.append(Thread(target=do_math,
19+
args=(30_000_000 * (n - 1) / processor_count,
20+
30_000_000 * n / processor_count),
21+
daemon=True)
22+
)
23+
24+
[t.start() for t in threads]
25+
[t.join() for t in threads]
26+
27+
dt = datetime.datetime.now() - t0
28+
print("Done in {:,.2f} sec. (factor: {:,.2f}x)".format(
29+
dt.total_seconds(),
30+
8.54/dt.total_seconds())
31+
)
32+
33+
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))
40+
41+
42+
if __name__ == '__main__':
43+
main()

src/11-cython/perf/math_core.pyx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from libc.math cimport sqrt
2+
3+
import cython
4+
5+
def do_math(start: cython.float = 0, num: cython.float = 10):
6+
pos: cython.float = start
7+
k_sq: cython.float = 1000 * 1000
8+
9+
with nogil:
10+
while pos < num:
11+
pos += 1
12+
sqrt((pos - k_sq) * (pos - k_sq))

src/11-cython/perf/setup.py

Lines changed: 6 additions & 0 deletions
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+
)

src/11-cython/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cython

0 commit comments

Comments
 (0)