Skip to content

Commit 497f92d

Browse files
committed
Add pi computation numba example
1 parent f4fab5d commit 497f92d

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

source-code/numba/computing_pi.ipynb

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "dad1f0e2-bc13-4d67-b40e-e5f8946e7e6f",
6+
"metadata": {},
7+
"source": [
8+
"# Requirements"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "694413b5-380c-4239-8bce-09b90df7fe79",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from numba import njit\n",
19+
"import numpy as np\n",
20+
"import random"
21+
]
22+
},
23+
{
24+
"cell_type": "markdown",
25+
"id": "aee5369d-7b69-4fb4-8567-52bd8e92571b",
26+
"metadata": {},
27+
"source": [
28+
"# Random $\\pi$"
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"id": "70202ae4-ad82-4c91-aea0-a3aeccfb7bdc",
34+
"metadata": {},
35+
"source": [
36+
"Compute $\\pi$ by generating random points in a square and counting how many there are in the circle inscribed in the square."
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 5,
42+
"id": "b5f095c0-58f6-4098-829c-6e696ae2a2bd",
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"def compute_pi(nr_tries):\n",
47+
" hits = 0\n",
48+
" for _ in range(nr_tries):\n",
49+
" x = random.random()\n",
50+
" y = random.random()\n",
51+
" if x**2 + y**2 < 1.0:\n",
52+
" hits += 1\n",
53+
" return 4.0*hits/nr_tries"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 6,
59+
"id": "805f4c9f-5d19-486a-988e-bf103683c37c",
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"@njit\n",
64+
"def compute_pi_jit(nr_tries):\n",
65+
" hits = 0\n",
66+
" for _ in range(nr_tries):\n",
67+
" x = random.random()\n",
68+
" y = random.random()\n",
69+
" if x**2 + y**2 < 1.0:\n",
70+
" hits += 1\n",
71+
" return 4.0*hits/nr_tries"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 32,
77+
"id": "f7a7bb7e-6ad1-4b6d-bb5b-d99ebedf7991",
78+
"metadata": {},
79+
"outputs": [],
80+
"source": [
81+
"@njit(['float64(int64)'])\n",
82+
"def compute_pi_jit_sign(nr_tries):\n",
83+
" hits = 0\n",
84+
" for _ in range(nr_tries):\n",
85+
" x = random.random()\n",
86+
" y = random.random()\n",
87+
" if x**2 + y**2 < 1.0:\n",
88+
" hits += 1\n",
89+
" return 4.0*hits/nr_tries"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 9,
95+
"id": "13f3c23d-674e-43b2-b503-a83c20cf5075",
96+
"metadata": {},
97+
"outputs": [
98+
{
99+
"name": "stdout",
100+
"output_type": "stream",
101+
"text": [
102+
"27.1 ms ± 277 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
103+
]
104+
}
105+
],
106+
"source": [
107+
"%timeit compute_pi(100_000)"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 10,
113+
"id": "de965fa5-b3e3-4548-8d41-661baf6abe65",
114+
"metadata": {},
115+
"outputs": [
116+
{
117+
"name": "stdout",
118+
"output_type": "stream",
119+
"text": [
120+
"687 µs ± 9.53 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
121+
]
122+
}
123+
],
124+
"source": [
125+
"%timeit compute_pi_jit(100_000)"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 34,
131+
"id": "f240d35d-2fdb-45db-9e59-d392887c9a16",
132+
"metadata": {},
133+
"outputs": [
134+
{
135+
"name": "stdout",
136+
"output_type": "stream",
137+
"text": [
138+
"685 µs ± 8.96 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
139+
]
140+
}
141+
],
142+
"source": [
143+
"%timeit compute_pi_jit_sign(np.int64(100_000))"
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"id": "da96c2f7-afc2-4122-ad80-62d7c57272e9",
149+
"metadata": {},
150+
"source": [
151+
"Using numba's just-in-time compiler significantly speeds up the computations."
152+
]
153+
}
154+
],
155+
"metadata": {
156+
"kernelspec": {
157+
"display_name": "Python 3 (ipykernel)",
158+
"language": "python",
159+
"name": "python3"
160+
},
161+
"language_info": {
162+
"codemirror_mode": {
163+
"name": "ipython",
164+
"version": 3
165+
},
166+
"file_extension": ".py",
167+
"mimetype": "text/x-python",
168+
"name": "python",
169+
"nbconvert_exporter": "python",
170+
"pygments_lexer": "ipython3",
171+
"version": "3.9.7"
172+
}
173+
},
174+
"nbformat": 4,
175+
"nbformat_minor": 5
176+
}

0 commit comments

Comments
 (0)