Skip to content

Commit f83aecb

Browse files
committed
Try a scalar/array variants of the pure numba impl
1 parent 08f1777 commit f83aecb

File tree

1 file changed

+83
-45
lines changed

1 file changed

+83
-45
lines changed

doc/gallery/scan/numba_fib_scan.ipynb

Lines changed: 83 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -38,126 +38,164 @@
3838
{
3939
"metadata": {
4040
"ExecuteTime": {
41-
"end_time": "2025-10-07T10:20:23.571190Z",
42-
"start_time": "2025-10-07T10:20:23.567707Z"
41+
"end_time": "2025-10-07T10:32:18.289971Z",
42+
"start_time": "2025-10-07T10:32:18.284515Z"
4343
}
4444
},
4545
"cell_type": "code",
4646
"source": [
4747
"import numba\n",
4848
"\n",
49-
"@numba.jit(nopython=True)\n",
50-
"def fibonacci_numba(b):\n",
49+
"@numba.njit\n",
50+
"def fibonacci_numba_scalar(b):\n",
5151
" b = b.copy()\n",
5252
" a = np.ones((), dtype=np.int32)\n",
5353
" for _ in range(N_STEPS):\n",
5454
" a[()], b[()] = a[()] + b[()], a[()]\n",
55+
" return a\n",
56+
"\n",
57+
"@numba.njit\n",
58+
"def fibonacci_numba_array(b):\n",
59+
" a = np.ones((), dtype=np.int32)\n",
60+
" for _ in range(N_STEPS):\n",
61+
" a, b = np.asarray(a + b), a\n",
5562
" return a"
5663
],
5764
"id": "b1d657d366647ada",
5865
"outputs": [],
59-
"execution_count": 2
66+
"execution_count": 66
6067
},
6168
{
6269
"metadata": {
6370
"ExecuteTime": {
64-
"end_time": "2025-10-07T10:20:26.947566Z",
65-
"start_time": "2025-10-07T10:20:23.615573Z"
71+
"end_time": "2025-10-07T10:32:19.607842Z",
72+
"start_time": "2025-10-07T10:32:19.423324Z"
6673
}
6774
},
6875
"cell_type": "code",
6976
"source": [
7077
"b = np.ones((), dtype=np.int32)\n",
71-
"assert fibonacci_pytensor(b) == fibonacci_numba(b)\n",
72-
"assert fibonacci_pytensor_numba(b) == fibonacci_numba(b)"
78+
"assert fibonacci_numba_array(b) == fibonacci_numba_scalar(b)"
7379
],
7480
"id": "7f45c87d259852e6",
7581
"outputs": [],
76-
"execution_count": 3
82+
"execution_count": 67
7783
},
7884
{
7985
"metadata": {
8086
"ExecuteTime": {
81-
"end_time": "2025-10-07T10:20:28.819553Z",
82-
"start_time": "2025-10-07T10:20:27.015141Z"
87+
"end_time": "2025-10-07T10:32:22.705191Z",
88+
"start_time": "2025-10-07T10:32:20.090353Z"
8389
}
8490
},
8591
"cell_type": "code",
86-
"source": "%timeit fibonacci_pytensor(b)",
87-
"id": "90f27f42b275e9da",
92+
"source": "%timeit fibonacci_numba_scalar(b)",
93+
"id": "b01c8978960c6e3d",
8894
"outputs": [
8995
{
9096
"name": "stdout",
9197
"output_type": "stream",
9298
"text": [
93-
"2.22 ms ± 37.8 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
99+
"3.21 μs ± 20.7 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n"
94100
]
95101
}
96102
],
97-
"execution_count": 4
103+
"execution_count": 68
98104
},
99105
{
100106
"metadata": {
101107
"ExecuteTime": {
102-
"end_time": "2025-10-07T10:20:42.302214Z",
103-
"start_time": "2025-10-07T10:20:28.871240Z"
108+
"end_time": "2025-10-07T10:32:25.876514Z",
109+
"start_time": "2025-10-07T10:32:23.122275Z"
104110
}
105111
},
106112
"cell_type": "code",
107-
"source": "%timeit fibonacci_pytensor_numba(b)",
108-
"id": "d918043168a39d59",
113+
"source": "%timeit fibonacci_numba_array(b)",
114+
"id": "bfc8794b219db03e",
109115
"outputs": [
110116
{
111117
"name": "stdout",
112118
"output_type": "stream",
113119
"text": [
114-
"165 μs ± 468 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
120+
"32.8 μs ± 2.48 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
115121
]
116122
}
117123
],
118-
"execution_count": 5
124+
"execution_count": 69
125+
},
126+
{
127+
"metadata": {},
128+
"cell_type": "code",
129+
"outputs": [],
130+
"execution_count": null,
131+
"source": [
132+
"assert fibonacci_pytensor(b) == fibonacci_numba_scalar(b)\n",
133+
"assert fibonacci_pytensor_numba(b) == fibonacci_numba_scalar(b)"
134+
],
135+
"id": "a2185c1de1297a11"
119136
},
120137
{
121138
"metadata": {
122139
"ExecuteTime": {
123-
"end_time": "2025-10-07T10:20:55.256172Z",
124-
"start_time": "2025-10-07T10:20:42.355007Z"
140+
"end_time": "2025-10-07T10:29:44.724064Z",
141+
"start_time": "2025-10-07T10:29:42.655693Z"
125142
}
126143
},
127144
"cell_type": "code",
128-
"source": "%timeit fibonacci_pytensor_numba.vm.jit_fn(b)",
129-
"id": "69ec1c01dad9fb66",
130145
"outputs": [
131146
{
132147
"name": "stdout",
133148
"output_type": "stream",
134149
"text": [
135-
"159 μs ± 1.41 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
150+
"2.49 ms ± 327 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
136151
]
137152
}
138153
],
139-
"execution_count": 6
154+
"execution_count": 54,
155+
"source": "%timeit fibonacci_pytensor(b)",
156+
"id": "f1e8bb6a0c673c8f"
140157
},
141158
{
142159
"metadata": {
143160
"ExecuteTime": {
144-
"end_time": "2025-10-07T10:20:57.954364Z",
145-
"start_time": "2025-10-07T10:20:55.346865Z"
161+
"end_time": "2025-10-07T10:29:58.922566Z",
162+
"start_time": "2025-10-07T10:29:44.752331Z"
146163
}
147164
},
148165
"cell_type": "code",
149-
"source": "%timeit fibonacci_numba(b)",
150-
"id": "b01c8978960c6e3d",
151166
"outputs": [
152167
{
153168
"name": "stdout",
154169
"output_type": "stream",
155170
"text": [
156-
"3.2 μs ± 19.2 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)\n"
171+
"175 μs ± 6.13 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
157172
]
158173
}
159174
],
160-
"execution_count": 7
175+
"execution_count": 55,
176+
"source": "%timeit fibonacci_pytensor_numba(b)",
177+
"id": "17cd2859b4c6d3bd"
178+
},
179+
{
180+
"metadata": {
181+
"ExecuteTime": {
182+
"end_time": "2025-10-07T10:30:11.832294Z",
183+
"start_time": "2025-10-07T10:29:59.016709Z"
184+
}
185+
},
186+
"cell_type": "code",
187+
"outputs": [
188+
{
189+
"name": "stdout",
190+
"output_type": "stream",
191+
"text": [
192+
"158 μs ± 706 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
193+
]
194+
}
195+
],
196+
"execution_count": 56,
197+
"source": "%timeit fibonacci_pytensor_numba.vm.jit_fn(b)",
198+
"id": "6deb056f63953a42"
161199
},
162200
{
163201
"metadata": {
@@ -362,8 +400,8 @@
362400
{
363401
"metadata": {
364402
"ExecuteTime": {
365-
"end_time": "2025-10-07T10:20:58.266498Z",
366-
"start_time": "2025-10-07T10:20:58.254087Z"
403+
"end_time": "2025-10-07T10:30:25.495418Z",
404+
"start_time": "2025-10-07T10:30:25.481386Z"
367405
}
368406
},
369407
"cell_type": "code",
@@ -458,29 +496,29 @@
458496
],
459497
"id": "bcefae049d4d2540",
460498
"outputs": [],
461-
"execution_count": 13
499+
"execution_count": 59
462500
},
463501
{
464502
"metadata": {
465503
"ExecuteTime": {
466-
"end_time": "2025-10-07T10:21:01.526942Z",
467-
"start_time": "2025-10-07T10:20:58.310079Z"
504+
"end_time": "2025-10-07T10:30:31.559493Z",
505+
"start_time": "2025-10-07T10:30:30.263832Z"
468506
}
469507
},
470508
"cell_type": "code",
471509
"source": [
472510
"b = np.ones((), dtype=np.int32)\n",
473-
"assert comparable_fibonacci_numba(b) == fibonacci_numba(b)"
511+
"assert comparable_fibonacci_numba(b) == fibonacci_numba_scalar(b)"
474512
],
475513
"id": "65887ebba21f46c3",
476514
"outputs": [],
477-
"execution_count": 14
515+
"execution_count": 60
478516
},
479517
{
480518
"metadata": {
481519
"ExecuteTime": {
482-
"end_time": "2025-10-07T10:21:06.041171Z",
483-
"start_time": "2025-10-07T10:21:01.578757Z"
520+
"end_time": "2025-10-07T10:30:35.999409Z",
521+
"start_time": "2025-10-07T10:30:31.567997Z"
484522
}
485523
},
486524
"cell_type": "code",
@@ -491,11 +529,11 @@
491529
"name": "stdout",
492530
"output_type": "stream",
493531
"text": [
494-
"55 μs ± 756 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
532+
"54.6 μs ± 1.28 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
495533
]
496534
}
497535
],
498-
"execution_count": 15
536+
"execution_count": 61
499537
},
500538
{
501539
"metadata": {

0 commit comments

Comments
 (0)