Skip to content

Commit 3896ae7

Browse files
committed
mypyc
1 parent e0e3382 commit 3896ae7

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

docs/mypyc-vs-cython.ipynb

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "b404a1ed-4939-4bac-843e-d46c3e4ec268",
6+
"metadata": {},
7+
"source": [
8+
"## Comparing Cython, mpypyc, and default Python speed\n",
9+
"\n",
10+
"On a simple thing:\n",
11+
"- mypyc gives a 10x speed-up\n",
12+
"- cython-3 a 40x speed-up"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 1,
18+
"id": "98f1413e-e664-4c0e-9182-556d7f192319",
19+
"metadata": {
20+
"tags": []
21+
},
22+
"outputs": [],
23+
"source": [
24+
"%load_ext mypyc_ipython"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 2,
30+
"id": "5286f9f6-347b-4c84-870f-677f32eb935d",
31+
"metadata": {
32+
"tags": []
33+
},
34+
"outputs": [],
35+
"source": [
36+
"def py_fibonacci(n: int) -> int:\n",
37+
" if n <= 1+1:\n",
38+
" return 1\n",
39+
" else:\n",
40+
" return py_fibonacci(n-1) + py_fibonacci(n-2)"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 3,
46+
"id": "a0083738-2588-4923-92b3-135038c9a248",
47+
"metadata": {
48+
"tags": []
49+
},
50+
"outputs": [],
51+
"source": [
52+
"%%mypyc\n",
53+
"def my_fibonacci(n: int) -> int:\n",
54+
" if n <= 2:\n",
55+
" return 1\n",
56+
" else:\n",
57+
" return my_fibonacci(n-1) + my_fibonacci(n-2)"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 4,
63+
"id": "af675113-798c-46cc-9ce0-b607420fe720",
64+
"metadata": {
65+
"tags": []
66+
},
67+
"outputs": [],
68+
"source": [
69+
"%load_ext cython"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 5,
75+
"id": "3060e007-0887-47c2-bcf2-f304ecace0be",
76+
"metadata": {
77+
"tags": []
78+
},
79+
"outputs": [],
80+
"source": [
81+
"%%cython\n",
82+
"cpdef int cy_fibonacci(int n):\n",
83+
" if n <= 2:\n",
84+
" return 1\n",
85+
" else:\n",
86+
" return cy_fibonacci(n-1) + cy_fibonacci(n-2)"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 6,
92+
"id": "444c2112-8c38-4064-b7df-3609375688fa",
93+
"metadata": {
94+
"tags": []
95+
},
96+
"outputs": [
97+
{
98+
"name": "stdout",
99+
"output_type": "stream",
100+
"text": [
101+
"101 ms ± 503 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
102+
]
103+
}
104+
],
105+
"source": [
106+
"%timeit py_fibonacci(30)"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 7,
112+
"id": "1bec3d97-229d-479f-b18d-99402c4a19d2",
113+
"metadata": {
114+
"tags": []
115+
},
116+
"outputs": [
117+
{
118+
"name": "stdout",
119+
"output_type": "stream",
120+
"text": [
121+
"8.55 ms ± 248 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
122+
]
123+
}
124+
],
125+
"source": [
126+
"%timeit my_fibonacci(30)"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": 8,
132+
"id": "66da0aba-d10d-4204-a8dd-90100aa495d8",
133+
"metadata": {
134+
"tags": []
135+
},
136+
"outputs": [
137+
{
138+
"name": "stdout",
139+
"output_type": "stream",
140+
"text": [
141+
"2.35 ms ± 75.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
142+
]
143+
}
144+
],
145+
"source": [
146+
"%timeit cy_fibonacci(30)"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": null,
152+
"id": "d2aa1e9d-a74b-4e00-9ea8-9f9baa12275b",
153+
"metadata": {},
154+
"outputs": [],
155+
"source": []
156+
}
157+
],
158+
"metadata": {
159+
"kernelspec": {
160+
"display_name": "Python 3 (ipykernel)",
161+
"language": "python",
162+
"name": "python3"
163+
},
164+
"language_info": {
165+
"codemirror_mode": {
166+
"name": "ipython",
167+
"version": 3
168+
},
169+
"file_extension": ".py",
170+
"mimetype": "text/x-python",
171+
"name": "python",
172+
"nbconvert_exporter": "python",
173+
"pygments_lexer": "ipython3",
174+
"version": "3.11.3"
175+
}
176+
},
177+
"nbformat": 4,
178+
"nbformat_minor": 5
179+
}

0 commit comments

Comments
 (0)