Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
win-pytreat
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
win-pytreat
Commits
42e37522
Commit
42e37522
authored
7 years ago
by
Michiel Cottaar
Browse files
Options
Downloads
Patches
Plain Diff
Updates to speeding up python talk
parent
d18421fb
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
talks/speed/speed.ipynb
+93
-1509
93 additions, 1509 deletions
talks/speed/speed.ipynb
with
93 additions
and
1509 deletions
talks/speed/speed.ipynb
+
93
−
1509
View file @
42e37522
...
...
@@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count":
17
,
"execution_count":
null
,
"metadata": {
"collapsed": true
},
...
...
@@ -20,20 +20,9 @@
},
{
"cell_type": "code",
"execution_count":
18
,
"execution_count":
null
,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((nan, nan), (-1.0, 4.0), (0.0, 0.0))"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"def root(a, b, c):\n",
" D = b ** 2 - 4 * a * c\n",
...
...
@@ -47,22 +36,9 @@
},
{
"cell_type": "code",
"
execution_count
"
:
19
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
ename
"
:
"
ValueError
",
"
evalue
"
:
"
The
truth
value
of
an
array
with
more
than
one
element
is
ambiguous.
Use
a.any
()
or
a.all
()",
"
traceback
"
:
[
"\
u001b
[0;31
m---------------------------------------------------------------------------
\
u001b
[0
m
",
"\
u001b
[0;31
mValueError
\
u001b
[0
m
Traceback
(
most
recent
call
last
)",
"\
u001b
[0;32
m
<
ipython-input-19-5d1fed3ed2df
>
\u001b[0m in \u001b[0;36m
<module>
\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1e6\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1e6\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mroot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m
<ipython-input-18-54b500cd66b1>
\u001b[0m in \u001b[0;36mroot\u001b[0;34m(a, b, c)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mroot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mD\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m**\u001b[0m \u001b[0;36m2\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m4\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0mD\u001b[0m \u001b[0;34m
<
\
u001b
[0
m
\
u001b
[0;36
m0
\
u001b
[0
m
\
u001b
[0;34
m:
\
u001b
[0
m
\
u001b
[0;34
m
\
u001b
[0
m
\
u001b
[0
m
\
n
\
u001b
[0
m
\
u001b
[1;32
m
4\
u001b
[0
m
\
u001b
[0;32
mreturn
\
u001b
[0
m
\
u001b
[0
mnp
\
u001b
[0
m
\
u001b
[0;34
m.
\
u001b
[0
m
\
u001b
[0
mnan
\
u001b
[0
m
\
u001b
[0;34
m
,\
u001b
[0
m
\
u001b
[0
mnp
\
u001b
[0
m
\
u001b
[0;34
m.
\
u001b
[0
m
\
u001b
[0
mnan
\
u001b
[0
m
\
u001b
[0;34
m
\
u001b
[0
m
\
u001b
[0
m
\
n
\
u001b
[1;32
m
5\
u001b
[0
m
\
u001b
[0
mx1
\
u001b
[0
m
\
u001b
[0;34
m=
\u001b[0m
\
u001b
[0;34
m
(\
u001b
[0
m
\
u001b
[0;34
m-
\
u001b
[0
m
\
u001b
[0
mb
\
u001b
[0
m
\
u001b
[0;34
m
+\
u001b
[0
m
\
u001b
[0
mnp
\
u001b
[0
m
\
u001b
[0;34
m.
\
u001b
[0
m
\
u001b
[0
msqrt
\
u001b
[0
m
\
u001b
[0;34
m
(\
u001b
[0
m
\
u001b
[0
mD
\
u001b
[0
m
\
u001b
[0;34
m
)\
u001b
[0
m
\
u001b
[0;34
m
)\
u001b
[0
m
\
u001b
[0;34
m
/\
u001b
[0
m
\
u001b
[0;34
m
(\
u001b
[0
m
\
u001b
[0;36
m2
\
u001b
[0
m
\
u001b
[0;34
m*
\
u001b
[0
m
\
u001b
[0
ma
\
u001b
[0
m
\
u001b
[0;34
m
)\
u001b
[0
m
\
u001b
[0;34
m
\
u001b
[0
m
\
u001b
[0
m
\
n
",
"\
u001b
[0;31
mValueError
\
u001b
[0
m:
The
truth
value
of
an
array
with
more
than
one
element
is
ambiguous.
Use
a.any
()
or
a.all
()"
],
"
output_type
"
:
"
error
"
}
],
"outputs": [],
"source": [
"a = np.random.randn(int(1e6))\n",
"b = np.random.randn(int(1e6))\n",
...
...
@@ -72,34 +48,9 @@
},
{
"cell_type": "code",
"
execution_count
"
:
20
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
3.64
s
,
sys:
65.9
ms
,
total:
3.71
s
\
n
",
"
Wall
time:
3.71
s
\
n
"
]
},
{
"
data
"
:
{
"
text
/
plain
"
:
[
"
array
([[
-0.55797188
,
1.11121928],\
n
",
"
[
nan
,
nan
],\
n
",
"
[
0.76166857,
-1.70637551
],\
n
",
"
...,\
n
",
"
[
-0.56859783
,
0.98659831],\
n
",
"
[
nan
,
nan
],\
n
",
"
[
-0.53531175
,
0.18339357]])"
]
},
"
execution_count
"
:
20,
"
metadata
"
:
{},
"
output_type
"
:
"
execute_result
"
}
],
"outputs": [],
"source": [
"%%time\n",
"np.array([root(av, bv, cv) for av, bv, cv in zip(a, b, c)])"
...
...
@@ -107,7 +58,7 @@
},
{
"cell_type": "code",
"
execution_count
"
:
21
,
"execution_count":
null
,
"metadata": {
"collapsed": true
},
...
...
@@ -119,31 +70,9 @@
},
{
"cell_type": "code",
"
execution_count
"
:
22
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
1.85
s
,
sys:
71.9
ms
,
total:
1.92
s
\
n
",
"
Wall
time:
1.92
s
\
n
"
]
},
{
"
data
"
:
{
"
text
/
plain
"
:
[
"(
array
([
-0.55797188
,
nan
,
0.76166857,
...,
-0.56859783
,\
n
",
"
nan
,
-0.53531175
]),\
n
",
"
array
([
1.11121928,
nan
,
-1.70637551
,
...,
0.98659831,\
n
",
"
nan
,
0.18339357]))"
]
},
"
execution_count
"
:
22,
"
metadata
"
:
{},
"
output_type
"
:
"
execute_result
"
}
],
"outputs": [],
"source": [
"%%time\n",
"root_vec(a, b, c)"
...
...
@@ -151,31 +80,9 @@
},
{
"cell_type": "code",
"
execution_count
"
:
23
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
1.66
s
,
sys:
62.7
ms
,
total:
1.73
s
\
n
",
"
Wall
time:
1.73
s
\
n
"
]
},
{
"
data
"
:
{
"
text
/
plain
"
:
[
"(
array
([
-1.72246968
,
-2.63787563
,
nan
,
...,
-2.20722406
,\
n
",
"
nan
,
-1.84415698
]),\
n
",
"
array
([2.27571708,
2.04353033,
nan
,
...,
2.62522454,
nan
,\
n
",
"
1.4922388
]))"
]
},
"
execution_count
"
:
23,
"
metadata
"
:
{},
"
output_type
"
:
"
execute_result
"
}
],
"outputs": [],
"source": [
"%%time\n",
"root_vec(a, b, 7)"
...
...
@@ -190,7 +97,7 @@
},
{
"cell_type": "code",
"
execution_count
"
:
24
,
"execution_count":
null
,
"metadata": {
"collapsed": true
},
...
...
@@ -201,7 +108,7 @@
},
{
"cell_type": "code",
"
execution_count
"
:
25
,
"execution_count":
null
,
"metadata": {
"collapsed": true
},
...
...
@@ -212,59 +119,27 @@
},
{
"cell_type": "code",
"
execution_count
"
:
26
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"3.08
µs
±
192
ns
per
loop
(
mean
±
std.
dev.
of
7
runs
,
100000
loops
each
)\
n
"
]
}
],
"outputs": [],
"source": [
"%timeit root(23, 78, 19.0)"
]
},
{
"cell_type": "code",
"
execution_count
"
:
27
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
The
slowest
run
took
11.09
times
longer
than
the
fastest.
This
could
mean
that
an
intermediate
result
is
being
cached.
\
n
",
"786
ns
±
1.08
µs
per
loop
(
mean
±
std.
dev.
of
7
runs
,
1
loop
each
)\
n
"
]
}
],
"outputs": [],
"source": [
"%timeit root_jit(23, 78, 19.0)"
]
},
{
"cell_type": "code",
"
execution_count
"
:
28
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
ename
"
:
"
SystemError
",
"
evalue
"
:
"
CPUDispatcher
(<
function
root
at
0
x114035620
>
) returned a result with an error set",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;31mValueError\u001b[0m: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()",
"\nThe above exception was the direct cause of the following exception:\n",
"\u001b[0;31mSystemError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m
<timed
eval
>
\u001b[0m in \u001b[0;36m
<module>
\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mSystemError\u001b[0m: CPUDispatcher(
<function
root
at
0
x114035620
>
) returned a result with an error set"
],
"output_type": "error"
}
],
"outputs": [],
"source": [
"%%time\n",
"root_jit(a, b, 7)"
...
...
@@ -272,31 +147,9 @@
},
{
"cell_type": "code",
"execution_count":
29
,
"execution_count":
null
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 406 ms, sys: 57.2 ms, total: 464 ms\n",
"Wall time: 464 ms\n"
]
},
{
"data": {
"text/plain": [
"(array([-1.72246968, -2.63787563, nan, ..., -2.20722406,\n",
" nan, -1.84415698]),\n",
" array([2.27571708, 2.04353033, nan, ..., 2.62522454, nan,\n",
" 1.4922388 ]))"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"root_jit_vec = np.vectorize(root_jit)\n",
"%time root_jit_vec(a, b, 7)"
...
...
@@ -304,31 +157,9 @@
},
{
"cell_type": "code",
"execution_count":
30
,
"execution_count":
null
,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 391 ms, sys: 58.2 ms, total: 449 ms\n",
"Wall time: 448 ms\n"
]
},
{
"data": {
"text/plain": [
"(array([-1.72246968, -2.63787563, nan, ..., -2.20722406,\n",
" nan, -1.84415698]),\n",
" array([2.27571708, 2.04353033, nan, ..., 2.62522454, nan,\n",
" 1.4922388 ]))"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"@np.vectorize\n",
"@numba.jit\n",
...
...
@@ -344,25 +175,9 @@
},
{
"cell_type": "code",
"
execution_count
"
:
31
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
ename
"
:
"
NotImplementedError
",
"
evalue
"
:
"(
float64
x
2)
cannot
be
represented
as
a
Numpy
dtype
",
"
traceback
"
:
[
"\
u001b
[0;31
m---------------------------------------------------------------------------
\
u001b
[0
m
",
"\
u001b
[0;31
mNotImplementedError
\
u001b
[0
m
Traceback
(
most
recent
call
last
)",
"\
u001b
[0;32
m
<
timed
eval
>
\u001b[0m in \u001b[0;36m
<module>
\u001b[0;34m()\u001b[0m\n",
"\u001b[0;32m~/miniconda3/lib/python3.6/site-packages/numba/npyufunc/dufunc.py\u001b[0m in \u001b[0;36m_compile_for_args\u001b[0;34m(self, *args, **kws)\u001b[0m\n\u001b[1;32m 166\u001b[0m \u001b[0margty\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0margty\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 167\u001b[0m \u001b[0margtys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margty\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 168\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compile_for_argtys\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtuple\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margtys\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 169\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 170\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_compile_for_argtys\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0margtys\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreturn_type\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/miniconda3/lib/python3.6/site-packages/numba/npyufunc/dufunc.py\u001b[0m in \u001b[0;36m_compile_for_argtys\u001b[0;34m(self, argtys, return_type)\u001b[0m\n\u001b[1;32m 188\u001b[0m cres, argtys, return_type)\n\u001b[1;32m 189\u001b[0m dtypenums, ptr, env = ufuncbuilder._build_element_wise_ufunc_wrapper(\n\u001b[0;32m--> 190\u001b[0;31m cres, actual_sig)\n\u001b[0m\u001b[1;32m 191\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_add_loop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlongint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mptr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtypenums\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 192\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_keepalive\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mptr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcres\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlibrary\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0menv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/miniconda3/lib/python3.6/site-packages/numba/npyufunc/ufuncbuilder.py\u001b[0m in \u001b[0;36m_build_element_wise_ufunc_wrapper\u001b[0;34m(cres, signature)\u001b[0m\n\u001b[1;32m 163\u001b[0m \u001b[0;31m# Get dtypes\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 164\u001b[0m \u001b[0mdtypenums\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mas_dtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnum\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0ma\u001b[0m \u001b[0;32min\u001b[0m \u001b[0msignature\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 165\u001b[0;31m \u001b[0mdtypenums\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mas_dtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msignature\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreturn_type\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 166\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdtypenums\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mptr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0menv\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 167\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/miniconda3/lib/python3.6/site-packages/numba/numpy_support.py\u001b[0m in \u001b[0;36mas_dtype\u001b[0;34m(nbtype)\u001b[0m\n\u001b[1;32m 134\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mas_dtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnbtype\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 135\u001b[0m raise NotImplementedError(\"%r cannot be represented as a Numpy dtype\"\n\u001b[0;32m--> 136\u001b[0;31m % (nbtype,))\n\u001b[0m\u001b[1;32m 137\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 138\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNotImplementedError\u001b[0m: (float64 x 2) cannot be represented as a Numpy dtype"
],
"output_type": "error"
}
],
"outputs": [],
"source": [
"@numba.vectorize\n",
"def fast_root(a, b, c):\n",
...
...
@@ -384,28 +199,9 @@
},
{
"cell_type": "code",
"
execution_count
"
:
32
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
1.56
s
,
sys:
3.5
ms
,
total:
1.56
s
\
n
",
"
Wall
time:
1.56
s
\
n
"
]
},
{
"
data
"
:
{
"
text
/
plain
"
:
[
"5702887"
]
},
"
execution_count
"
:
32,
"
metadata
"
:
{},
"
output_type
"
:
"
execute_result
"
}
],
"outputs": [],
"source": [
"def fib(n):\n",
" if n <= 1:\n",
...
...
@@ -417,28 +213,9 @@
},
{
"cell_type": "code",
"
execution_count
"
:
35
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
63.4
ms
,
sys:
1.79
ms
,
total:
65.2
ms
\
n
",
"
Wall
time:
64.2
ms
\
n
"
]
},
{
"
data
"
:
{
"
text
/
plain
"
:
[
"5702887"
]
},
"
execution_count
"
:
35,
"
metadata
"
:
{},
"
output_type
"
:
"
execute_result
"
}
],
"outputs": [],
"source": [
"@numba.jit\n",
"def fib_jit(n):\n",
...
...
@@ -451,80 +228,36 @@
},
{
"cell_type": "code",
"
execution_count
"
:
34
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
1.28
s
,
sys:
5.26
ms
,
total:
1.28
s
\
n
",
"
Wall
time:
1.28
s
\
n
"
]
},
{
"
data
"
:
{
"
text
/
plain
"
:
[
"267914296"
]
},
"
execution_count
"
:
34,
"
metadata
"
:
{},
"
output_type
"
:
"
execute_result
"
}
],
"outputs": [],
"source": [
"%time fib_jit(41)"
]
},
{
"cell_type": "code",
"
execution_count
"
:
103
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
"
]
}
],
"outputs": [],
"source": [
"%
prun
fib
(33
)"
"%prun fib
_jit(41
)"
]
},
{
"cell_type": "code",
"
execution_count
"
:
104
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
"
]
}
],
"outputs": [],
"source": [
"%
prun
fib
_jit
(41
)"
"%prun fib
(33
)"
]
},
{
"cell_type": "code",
"
execution_count
"
:
112
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
20
µs
,
sys:
1
µs
,
total:
21
µs
\
n
",
"
Wall
time:
24.1
µs
\
n
"
]
}
],
"outputs": [],
"source": [
"from functools import lru_cache\n",
"@lru_cache(None)\n",
...
...
@@ -539,38 +272,8 @@
{
"cell_type": "code",
"execution_count": null,
"
metadata
"
:
{
"
collapsed
"
:
true
},
"
outputs
"
:
[],
"
source
"
:
[
"
lru_cache
?"
]
},
{
"
cell_type
"
:
"
code
",
"
execution_count
"
:
111,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
261
µs
,
sys:
1
e
+03
ns
,
total:
262
µs
\
n
",
"
Wall
time:
267
µs
\
n
"
]
},
{
"
data
"
:
{
"
text
/
plain
"
:
[
"225591516161936330872512695036072072046011324913758190588638866418474627738686883405015987052796968498626"
]
},
"
execution_count
"
:
111,
"
metadata
"
:
{},
"
output_type
"
:
"
execute_result
"
}
],
"outputs": [],
"source": [
"%time fib_cache(500)"
]
...
...
@@ -591,17 +294,9 @@
},
{
"cell_type": "code",
"
execution_count
"
:
48
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"139
ms
±
1.16
ms
per
loop
(
mean
±
std.
dev.
of
7
runs
,
10
loops
each
)\
n
"
]
}
],
"outputs": [],
"source": [
"a = np.random.randn(int(1e7))\n",
"b = np.random.randn(int(1e7))\n",
...
...
@@ -610,17 +305,9 @@
},
{
"cell_type": "code",
"
execution_count
"
:
49
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"11.4
ms
±
484
µs
per
loop
(
mean
±
std.
dev.
of
7
runs
,
100
loops
each
)\
n
"
]
}
],
"outputs": [],
"source": [
"import numexpr as ne\n",
"%timeit ne.evaluate('a * b + 4.1 * a < 3 * b')"
...
...
@@ -635,36 +322,21 @@
},
{
"cell_type": "code",
"
execution_count
"
:
54
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"141
ms
±
1
ms
per
loop
(
mean
±
std.
dev.
of
7
runs
,
10
loops
each
)\
n
"
]
}
],
"outputs": [],
"source": [
"%
timeit
a
*
np.sqrt
(
abs
(
b
))
<
3
*
b
"
"c = np.random.randn(int(1e7))\n",
"%timeit (-b - np.sqrt(b ** 2 - 4 * a * c) ) / (2 * a)"
]
},
{
"cell_type": "code",
"
execution_count
"
:
55
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"12.6
ms
±
986
µs
per
loop
(
mean
±
std.
dev.
of
7
runs
,
100
loops
each
)\
n
"
]
}
],
"outputs": [],
"source": [
"%
timeit
ne.evaluate
('
a
*
sqrt
(
abs
(
b
))
<
3
*
b
')"
"%timeit ne.evaluate('
(-b -
sqrt(
b ** 2 - 4 * a * c) ) / (2
*
a)
')"
]
},
{
...
...
@@ -687,37 +359,18 @@
},
{
"cell_type": "code",
"
execution_count
"
:
69
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
The
cython
extension
is
already
loaded.
To
reload
it
,
use:
\
n
",
"
%
reload_ext
cython
\
n
"
]
}
],
"outputs": [],
"source": [
"%load_ext cython"
]
},
{
"cell_type": "code",
"
execution_count
"
:
70
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"[2,
3,
5,
7,
11,
13,
17,
19,
23,
29]\
n
",
"
CPU
times:
user
67.4
ms
,
sys:
1.22
ms
,
total:
68.6
ms
\
n
",
"
Wall
time:
68
ms
\
n
"
]
}
],
"outputs": [],
"source": [
"def primes(kmax):\n",
" p = {}\n",
...
...
@@ -735,528 +388,23 @@
" k = k + 1\n",
" result.append(n)\n",
" n = n + 1\n",
"
return
result
"
" return result\n",
"print(primes(10))\n",
"%time _ = primes(10000)"
]
},
{
"cell_type": "code",
"
execution_count
"
:
91
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
data
"
:
{
"
text
/
html
"
:
[
"<!
DOCTYPE
html
>
\n",
"
<!-- Generated by Cython 0.26 -->
\n",
"
<html>
\n",
"
<head>
\n",
"
<meta
http-equiv=
\"Content-Type\"
content=
\"text/html;
charset=
utf-8\"
/>
\n",
"
<title>
Cython: _cython_magic_52e640a266384bf0478136fe7ab4a75d.pyx
</title>
\n",
"
<style
type=
\"text/css\"
>
\
n
",
"
\
n
",
"
body
.cython
{
font-family
:
courier
;
font-size
:
12
;
}
\
n
",
"
\
n
",
"
.cython.tag
{
}
\
n
",
"
.cython.line
{
margin
:
0em
}
\
n
",
"
.cython.code
{
font-size
:
9
;
color
:
#444444
;
display
:
none
;
margin
:
0px
0px
0px
8px
;
border-left
:
8px
none
;
}
\
n
",
"
\
n
",
"
.cython.line
.run
{
background-color
:
#B0FFB0
;
}
\
n
",
"
.cython.line
.mis
{
background-color
:
#FFB0B0
;
}
\
n
",
"
.cython.code.run
{
border-left
:
8px
solid
#B0FFB0
;
}
\
n
",
"
.cython.code.mis
{
border-left
:
8px
solid
#FFB0B0
;
}
\
n
",
"
\
n
",
"
.cython.code
.py_c_api
{
color
:
red
;
}
\
n
",
"
.cython.code
.py_macro_api
{
color
:
#FF7000
;
}
\
n
",
"
.cython.code
.pyx_c_api
{
color
:
#FF3000
;
}
\
n
",
"
.cython.code
.pyx_macro_api
{
color
:
#FF7000
;
}
\
n
",
"
.cython.code
.refnanny
{
color
:
#FFA000
;
}
\
n
",
"
.cython.code
.trace
{
color
:
#FFA000
;
}
\
n
",
"
.cython.code
.error_goto
{
color
:
#FFA000
;
}
\
n
",
"
\
n
",
"
.cython.code
.coerce
{
color
:
#008000
;
border
:
1px
dotted
#008000
}
\
n
",
"
.cython.code
.py_attr
{
color
:
#FF0000
;
font-weight
:
bold
;
}
\
n
",
"
.cython.code
.c_attr
{
color
:
#0000FF
;
}
\
n
",
"
.cython.code
.py_call
{
color
:
#FF0000
;
font-weight
:
bold
;
}
\
n
",
"
.cython.code
.c_call
{
color
:
#0000FF
;
}
\
n
",
"
\
n
",
"
.cython.score-0
{
background-color
:
#FFFFff
;}
\
n
",
"
.cython.score-1
{
background-color
:
#FFFFe7
;}
\
n
",
"
.cython.score-2
{
background-color
:
#FFFFd4
;}
\
n
",
"
.cython.score-3
{
background-color
:
#FFFFc4
;}
\
n
",
"
.cython.score-4
{
background-color
:
#FFFFb6
;}
\
n
",
"
.cython.score-5
{
background-color
:
#FFFFaa
;}
\
n
",
"
.cython.score-6
{
background-color
:
#FFFF9f
;}
\
n
",
"
.cython.score-7
{
background-color
:
#FFFF96
;}
\
n
",
"
.cython.score-8
{
background-color
:
#FFFF8d
;}
\
n
",
"
.cython.score-9
{
background-color
:
#FFFF86
;}
\
n
",
"
.cython.score-10
{
background-color
:
#FFFF7f
;}
\
n
",
"
.cython.score-11
{
background-color
:
#FFFF79
;}
\
n
",
"
.cython.score-12
{
background-color
:
#FFFF73
;}
\
n
",
"
.cython.score-13
{
background-color
:
#FFFF6e
;}
\
n
",
"
.cython.score-14
{
background-color
:
#FFFF6a
;}
\
n
",
"
.cython.score-15
{
background-color
:
#FFFF66
;}
\
n
",
"
.cython.score-16
{
background-color
:
#FFFF62
;}
\
n
",
"
.cython.score-17
{
background-color
:
#FFFF5e
;}
\
n
",
"
.cython.score-18
{
background-color
:
#FFFF5b
;}
\
n
",
"
.cython.score-19
{
background-color
:
#FFFF57
;}
\
n
",
"
.cython.score-20
{
background-color
:
#FFFF55
;}
\
n
",
"
.cython.score-21
{
background-color
:
#FFFF52
;}
\
n
",
"
.cython.score-22
{
background-color
:
#FFFF4f
;}
\
n
",
"
.cython.score-23
{
background-color
:
#FFFF4d
;}
\
n
",
"
.cython.score-24
{
background-color
:
#FFFF4b
;}
\
n
",
"
.cython.score-25
{
background-color
:
#FFFF48
;}
\
n
",
"
.cython.score-26
{
background-color
:
#FFFF46
;}
\
n
",
"
.cython.score-27
{
background-color
:
#FFFF44
;}
\
n
",
"
.cython.score-28
{
background-color
:
#FFFF43
;}
\
n
",
"
.cython.score-29
{
background-color
:
#FFFF41
;}
\
n
",
"
.cython.score-30
{
background-color
:
#FFFF3f
;}
\
n
",
"
.cython.score-31
{
background-color
:
#FFFF3e
;}
\
n
",
"
.cython.score-32
{
background-color
:
#FFFF3c
;}
\
n
",
"
.cython.score-33
{
background-color
:
#FFFF3b
;}
\
n
",
"
.cython.score-34
{
background-color
:
#FFFF39
;}
\
n
",
"
.cython.score-35
{
background-color
:
#FFFF38
;}
\
n
",
"
.cython.score-36
{
background-color
:
#FFFF37
;}
\
n
",
"
.cython.score-37
{
background-color
:
#FFFF36
;}
\
n
",
"
.cython.score-38
{
background-color
:
#FFFF35
;}
\
n
",
"
.cython.score-39
{
background-color
:
#FFFF34
;}
\
n
",
"
.cython.score-40
{
background-color
:
#FFFF33
;}
\
n
",
"
.cython.score-41
{
background-color
:
#FFFF32
;}
\
n
",
"
.cython.score-42
{
background-color
:
#FFFF31
;}
\
n
",
"
.cython.score-43
{
background-color
:
#FFFF30
;}
\
n
",
"
.cython.score-44
{
background-color
:
#FFFF2f
;}
\
n
",
"
.cython.score-45
{
background-color
:
#FFFF2e
;}
\
n
",
"
.cython.score-46
{
background-color
:
#FFFF2d
;}
\
n
",
"
.cython.score-47
{
background-color
:
#FFFF2c
;}
\
n
",
"
.cython.score-48
{
background-color
:
#FFFF2b
;}
\
n
",
"
.cython.score-49
{
background-color
:
#FFFF2b
;}
\
n
",
"
.cython.score-50
{
background-color
:
#FFFF2a
;}
\
n
",
"
.cython.score-51
{
background-color
:
#FFFF29
;}
\
n
",
"
.cython.score-52
{
background-color
:
#FFFF29
;}
\
n
",
"
.cython.score-53
{
background-color
:
#FFFF28
;}
\
n
",
"
.cython.score-54
{
background-color
:
#FFFF27
;}
\
n
",
"
.cython.score-55
{
background-color
:
#FFFF27
;}
\
n
",
"
.cython.score-56
{
background-color
:
#FFFF26
;}
\
n
",
"
.cython.score-57
{
background-color
:
#FFFF26
;}
\
n
",
"
.cython.score-58
{
background-color
:
#FFFF25
;}
\
n
",
"
.cython.score-59
{
background-color
:
#FFFF24
;}
\
n
",
"
.cython.score-60
{
background-color
:
#FFFF24
;}
\
n
",
"
.cython.score-61
{
background-color
:
#FFFF23
;}
\
n
",
"
.cython.score-62
{
background-color
:
#FFFF23
;}
\
n
",
"
.cython.score-63
{
background-color
:
#FFFF22
;}
\
n
",
"
.cython.score-64
{
background-color
:
#FFFF22
;}
\
n
",
"
.cython.score-65
{
background-color
:
#FFFF22
;}
\
n
",
"
.cython.score-66
{
background-color
:
#FFFF21
;}
\
n
",
"
.cython.score-67
{
background-color
:
#FFFF21
;}
\
n
",
"
.cython.score-68
{
background-color
:
#FFFF20
;}
\
n
",
"
.cython.score-69
{
background-color
:
#FFFF20
;}
\
n
",
"
.cython.score-70
{
background-color
:
#FFFF1f
;}
\
n
",
"
.cython.score-71
{
background-color
:
#FFFF1f
;}
\
n
",
"
.cython.score-72
{
background-color
:
#FFFF1f
;}
\
n
",
"
.cython.score-73
{
background-color
:
#FFFF1e
;}
\
n
",
"
.cython.score-74
{
background-color
:
#FFFF1e
;}
\
n
",
"
.cython.score-75
{
background-color
:
#FFFF1e
;}
\
n
",
"
.cython.score-76
{
background-color
:
#FFFF1d
;}
\
n
",
"
.cython.score-77
{
background-color
:
#FFFF1d
;}
\
n
",
"
.cython.score-78
{
background-color
:
#FFFF1c
;}
\
n
",
"
.cython.score-79
{
background-color
:
#FFFF1c
;}
\
n
",
"
.cython.score-80
{
background-color
:
#FFFF1c
;}
\
n
",
"
.cython.score-81
{
background-color
:
#FFFF1c
;}
\
n
",
"
.cython.score-82
{
background-color
:
#FFFF1b
;}
\
n
",
"
.cython.score-83
{
background-color
:
#FFFF1b
;}
\
n
",
"
.cython.score-84
{
background-color
:
#FFFF1b
;}
\
n
",
"
.cython.score-85
{
background-color
:
#FFFF1a
;}
\
n
",
"
.cython.score-86
{
background-color
:
#FFFF1a
;}
\
n
",
"
.cython.score-87
{
background-color
:
#FFFF1a
;}
\
n
",
"
.cython.score-88
{
background-color
:
#FFFF1a
;}
\
n
",
"
.cython.score-89
{
background-color
:
#FFFF19
;}
\
n
",
"
.cython.score-90
{
background-color
:
#FFFF19
;}
\
n
",
"
.cython.score-91
{
background-color
:
#FFFF19
;}
\
n
",
"
.cython.score-92
{
background-color
:
#FFFF19
;}
\
n
",
"
.cython.score-93
{
background-color
:
#FFFF18
;}
\
n
",
"
.cython.score-94
{
background-color
:
#FFFF18
;}
\
n
",
"
.cython.score-95
{
background-color
:
#FFFF18
;}
\
n
",
"
.cython.score-96
{
background-color
:
#FFFF18
;}
\
n
",
"
.cython.score-97
{
background-color
:
#FFFF17
;}
\
n
",
"
.cython.score-98
{
background-color
:
#FFFF17
;}
\
n
",
"
.cython.score-99
{
background-color
:
#FFFF17
;}
\
n
",
"
.cython.score-100
{
background-color
:
#FFFF17
;}
\
n
",
"
.cython.score-101
{
background-color
:
#FFFF16
;}
\
n
",
"
.cython.score-102
{
background-color
:
#FFFF16
;}
\
n
",
"
.cython.score-103
{
background-color
:
#FFFF16
;}
\
n
",
"
.cython.score-104
{
background-color
:
#FFFF16
;}
\
n
",
"
.cython.score-105
{
background-color
:
#FFFF16
;}
\
n
",
"
.cython.score-106
{
background-color
:
#FFFF15
;}
\
n
",
"
.cython.score-107
{
background-color
:
#FFFF15
;}
\
n
",
"
.cython.score-108
{
background-color
:
#FFFF15
;}
\
n
",
"
.cython.score-109
{
background-color
:
#FFFF15
;}
\
n
",
"
.cython.score-110
{
background-color
:
#FFFF15
;}
\
n
",
"
.cython.score-111
{
background-color
:
#FFFF15
;}
\
n
",
"
.cython.score-112
{
background-color
:
#FFFF14
;}
\
n
",
"
.cython.score-113
{
background-color
:
#FFFF14
;}
\
n
",
"
.cython.score-114
{
background-color
:
#FFFF14
;}
\
n
",
"
.cython.score-115
{
background-color
:
#FFFF14
;}
\
n
",
"
.cython.score-116
{
background-color
:
#FFFF14
;}
\
n
",
"
.cython.score-117
{
background-color
:
#FFFF14
;}
\
n
",
"
.cython.score-118
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-119
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-120
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-121
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-122
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-123
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-124
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-125
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-126
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-127
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-128
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-129
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-130
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-131
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-132
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-133
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-134
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-135
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-136
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-137
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-138
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-139
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-140
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-141
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-142
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-143
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-144
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-145
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-146
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-147
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-148
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-149
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-150
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-151
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-152
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-153
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-154
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-155
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-156
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-157
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-158
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-159
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-160
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-161
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-162
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-163
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-164
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-165
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-166
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-167
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-168
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-169
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-170
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-171
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-172
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-173
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-174
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-175
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-176
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-177
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-178
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-179
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-180
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-181
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-182
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-183
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-184
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-185
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-186
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-187
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-188
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-189
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-190
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-191
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-192
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-193
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-194
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-195
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-196
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-197
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-198
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-199
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-200
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-201
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-202
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-203
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-204
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-205
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-206
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-207
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-208
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-209
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-210
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-211
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-212
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-213
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-214
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-215
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-216
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-217
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-218
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-219
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-220
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-221
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-222
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-223
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-224
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-225
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-226
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-227
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-228
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-229
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-230
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-231
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-232
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-233
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-234
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-235
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-236
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-237
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-238
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-239
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-240
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-241
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-242
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-243
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-244
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-245
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-246
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-247
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-248
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-249
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-250
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-251
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-252
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-253
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-254
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython
.hll
{
background-color
:
#ffffcc
}
\
n
",
"
.cython
{
background
:
#f8f8f8
;
}
\
n
",
"
.cython
.c
{
color
:
#408080
;
font-style
:
italic
}
/* Comment */
\
n
",
"
.cython
.err
{
border
:
1px
solid
#FF0000
}
/* Error */
\
n
",
"
.cython
.k
{
color
:
#008000
;
font-weight
:
bold
}
/* Keyword */
\
n
",
"
.cython
.o
{
color
:
#666666
}
/* Operator */
\
n
",
"
.cython
.ch
{
color
:
#408080
;
font-style
:
italic
}
/* Comment.Hashbang */
\
n
",
"
.cython
.cm
{
color
:
#408080
;
font-style
:
italic
}
/* Comment.Multiline */
\
n
",
"
.cython
.cp
{
color
:
#BC7A00
}
/* Comment.Preproc */
\
n
",
"
.cython
.cpf
{
color
:
#408080
;
font-style
:
italic
}
/* Comment.PreprocFile */
\
n
",
"
.cython
.c1
{
color
:
#408080
;
font-style
:
italic
}
/* Comment.Single */
\
n
",
"
.cython
.cs
{
color
:
#408080
;
font-style
:
italic
}
/* Comment.Special */
\
n
",
"
.cython
.gd
{
color
:
#A00000
}
/* Generic.Deleted */
\
n
",
"
.cython
.ge
{
font-style
:
italic
}
/* Generic.Emph */
\
n
",
"
.cython
.gr
{
color
:
#FF0000
}
/* Generic.Error */
\
n
",
"
.cython
.gh
{
color
:
#000080
;
font-weight
:
bold
}
/* Generic.Heading */
\
n
",
"
.cython
.gi
{
color
:
#00A000
}
/* Generic.Inserted */
\
n
",
"
.cython
.go
{
color
:
#888888
}
/* Generic.Output */
\
n
",
"
.cython
.gp
{
color
:
#000080
;
font-weight
:
bold
}
/* Generic.Prompt */
\
n
",
"
.cython
.gs
{
font-weight
:
bold
}
/* Generic.Strong */
\
n
",
"
.cython
.gu
{
color
:
#800080
;
font-weight
:
bold
}
/* Generic.Subheading */
\
n
",
"
.cython
.gt
{
color
:
#0044DD
}
/* Generic.Traceback */
\
n
",
"
.cython
.kc
{
color
:
#008000
;
font-weight
:
bold
}
/* Keyword.Constant */
\
n
",
"
.cython
.kd
{
color
:
#008000
;
font-weight
:
bold
}
/* Keyword.Declaration */
\
n
",
"
.cython
.kn
{
color
:
#008000
;
font-weight
:
bold
}
/* Keyword.Namespace */
\
n
",
"
.cython
.kp
{
color
:
#008000
}
/* Keyword.Pseudo */
\
n
",
"
.cython
.kr
{
color
:
#008000
;
font-weight
:
bold
}
/* Keyword.Reserved */
\
n
",
"
.cython
.kt
{
color
:
#B00040
}
/* Keyword.Type */
\
n
",
"
.cython
.m
{
color
:
#666666
}
/* Literal.Number */
\
n
",
"
.cython
.s
{
color
:
#BA2121
}
/* Literal.String */
\
n
",
"
.cython
.na
{
color
:
#7D9029
}
/* Name.Attribute */
\
n
",
"
.cython
.nb
{
color
:
#008000
}
/* Name.Builtin */
\
n
",
"
.cython
.nc
{
color
:
#0000FF
;
font-weight
:
bold
}
/* Name.Class */
\
n
",
"
.cython
.no
{
color
:
#880000
}
/* Name.Constant */
\
n
",
"
.cython
.nd
{
color
:
#AA22FF
}
/* Name.Decorator */
\
n
",
"
.cython
.ni
{
color
:
#999999
;
font-weight
:
bold
}
/* Name.Entity */
\
n
",
"
.cython
.ne
{
color
:
#D2413A
;
font-weight
:
bold
}
/* Name.Exception */
\
n
",
"
.cython
.nf
{
color
:
#0000FF
}
/* Name.Function */
\
n
",
"
.cython
.nl
{
color
:
#A0A000
}
/* Name.Label */
\
n
",
"
.cython
.nn
{
color
:
#0000FF
;
font-weight
:
bold
}
/* Name.Namespace */
\
n
",
"
.cython
.nt
{
color
:
#008000
;
font-weight
:
bold
}
/* Name.Tag */
\
n
",
"
.cython
.nv
{
color
:
#19177C
}
/* Name.Variable */
\
n
",
"
.cython
.ow
{
color
:
#AA22FF
;
font-weight
:
bold
}
/* Operator.Word */
\
n
",
"
.cython
.w
{
color
:
#bbbbbb
}
/* Text.Whitespace */
\
n
",
"
.cython
.mb
{
color
:
#666666
}
/* Literal.Number.Bin */
\
n
",
"
.cython
.mf
{
color
:
#666666
}
/* Literal.Number.Float */
\
n
",
"
.cython
.mh
{
color
:
#666666
}
/* Literal.Number.Hex */
\
n
",
"
.cython
.mi
{
color
:
#666666
}
/* Literal.Number.Integer */
\
n
",
"
.cython
.mo
{
color
:
#666666
}
/* Literal.Number.Oct */
\
n
",
"
.cython
.sa
{
color
:
#BA2121
}
/* Literal.String.Affix */
\
n
",
"
.cython
.sb
{
color
:
#BA2121
}
/* Literal.String.Backtick */
\
n
",
"
.cython
.sc
{
color
:
#BA2121
}
/* Literal.String.Char */
\
n
",
"
.cython
.dl
{
color
:
#BA2121
}
/* Literal.String.Delimiter */
\
n
",
"
.cython
.sd
{
color
:
#BA2121
;
font-style
:
italic
}
/* Literal.String.Doc */
\
n
",
"
.cython
.s2
{
color
:
#BA2121
}
/* Literal.String.Double */
\
n
",
"
.cython
.se
{
color
:
#BB6622
;
font-weight
:
bold
}
/* Literal.String.Escape */
\
n
",
"
.cython
.sh
{
color
:
#BA2121
}
/* Literal.String.Heredoc */
\
n
",
"
.cython
.si
{
color
:
#BB6688
;
font-weight
:
bold
}
/* Literal.String.Interpol */
\
n
",
"
.cython
.sx
{
color
:
#008000
}
/* Literal.String.Other */
\
n
",
"
.cython
.sr
{
color
:
#BB6688
}
/* Literal.String.Regex */
\
n
",
"
.cython
.s1
{
color
:
#BA2121
}
/* Literal.String.Single */
\
n
",
"
.cython
.ss
{
color
:
#19177C
}
/* Literal.String.Symbol */
\
n
",
"
.cython
.bp
{
color
:
#008000
}
/* Name.Builtin.Pseudo */
\
n
",
"
.cython
.fm
{
color
:
#0000FF
}
/* Name.Function.Magic */
\
n
",
"
.cython
.vc
{
color
:
#19177C
}
/* Name.Variable.Class */
\
n
",
"
.cython
.vg
{
color
:
#19177C
}
/* Name.Variable.Global */
\
n
",
"
.cython
.vi
{
color
:
#19177C
}
/* Name.Variable.Instance */
\
n
",
"
.cython
.vm
{
color
:
#19177C
}
/* Name.Variable.Magic */
\
n
",
"
.cython
.il
{
color
:
#666666
}
/* Literal.Number.Integer.Long */
\
n
",
"
</style>
\n",
"
<script>
\
n
"
,
"
function
toggleDiv
(
id
)
{
\
n
"
,
"
theDiv
=
id
.
nextElementSibling
\
n
"
,
"
if
(
theDiv
.
style
.
display
!=
'
block
'
)
theDiv
.
style
.
display
=
'
block
'
;
\
n
"
,
"
else
theDiv
.
style
.
display
=
'
none
'
;
\
n
"
,
"
}
\
n
"
,
"
</script>
\n",
"
</head>
\n",
"
<body
class=
\"cython\"
>
\n",
"
<p><span
style=
\"border-bottom:
solid
1
px
grey
;\"
>
Generated by Cython 0.26
</span></p>
\n",
"
<p>
\n",
"
<span
style=
\"background-color:
#FFFF00
\"
>
Yellow lines
</span>
hint at Python interaction.
<br
/>
\n",
" Click on a line that starts with a \"
<code>
+
</code>
\" to see the C code that Cython generated for it.\n",
"
</p>
\n",
"
<div
class=
\"cython\"
><pre
class=
\"cython
line
score-24
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
01
</span>
:
<span
class=
\"k\"
>
def
</span>
<span
class=
\"nf\"
>
cprimes
</span><span
class=
\"p\"
>
(
</span><span
class=
\"nb\"
>
int
</span>
<span
class=
\"n\"
>
kmax
</span><span
class=
\"p\"
>
):
</span></pre>
\n",
"
<pre
class=
'cython code score-24 '
>
/* Python wrapper */\n",
"static PyObject *__pyx_pw_46_cython_magic_52e640a266384bf0478136fe7ab4a75d_1cprimes(PyObject *__pyx_self, PyObject *__pyx_arg_kmax); /*proto*/\n",
"static PyMethodDef __pyx_mdef_46_cython_magic_52e640a266384bf0478136fe7ab4a75d_1cprimes = {\"cprimes\", (PyCFunction)__pyx_pw_46_cython_magic_52e640a266384bf0478136fe7ab4a75d_1cprimes, METH_O, 0};\n",
"static PyObject *__pyx_pw_46_cython_magic_52e640a266384bf0478136fe7ab4a75d_1cprimes(PyObject *__pyx_self, PyObject *__pyx_arg_kmax) {\n",
" int __pyx_v_kmax;\n",
" PyObject *__pyx_r = 0;\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannyDeclarations
</span>
\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannySetupContext
</span>
(\"cprimes (wrapper)\", 0);\n",
" assert(__pyx_arg_kmax); {\n",
" __pyx_v_kmax =
<span
class=
'pyx_c_api'
>
__Pyx_PyInt_As_int
</span>
(__pyx_arg_kmax); if (unlikely((__pyx_v_kmax == (int)-1)
&&
<span
class=
'py_c_api'
>
PyErr_Occurred
</span>
())) __PYX_ERR(0, 1, __pyx_L3_error)\n",
" }\n",
" goto __pyx_L4_argument_unpacking_done;\n",
" __pyx_L3_error:;\n",
"
<span
class=
'pyx_c_api'
>
__Pyx_AddTraceback
</span>
(\"_cython_magic_52e640a266384bf0478136fe7ab4a75d.cprimes\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannyFinishContext
</span>
();\n",
" return NULL;\n",
" __pyx_L4_argument_unpacking_done:;\n",
" __pyx_r = __pyx_pf_46_cython_magic_52e640a266384bf0478136fe7ab4a75d_cprimes(__pyx_self, ((int)__pyx_v_kmax));\n",
"\n",
" /* function exit code */\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannyFinishContext
</span>
();\n",
" return __pyx_r;\n",
"}\n",
"\n",
"static PyObject *__pyx_pf_46_cython_magic_52e640a266384bf0478136fe7ab4a75d_cprimes(CYTHON_UNUSED PyObject *__pyx_self, int __pyx_v_kmax) {\n",
" int __pyx_v_p[0x3E8];\n",
" PyObject *__pyx_v_result = NULL;\n",
" int __pyx_v_k;\n",
" int __pyx_v_n;\n",
" int __pyx_v_i;\n",
" PyObject *__pyx_r = NULL;\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannyDeclarations
</span>
\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannySetupContext
</span>
(\"cprimes\", 0);\n",
"/* … */\n",
" /* function exit code */\n",
" __pyx_L1_error:;\n",
"
<span
class=
'pyx_macro_api'
>
__Pyx_XDECREF
</span>
(__pyx_t_1);\n",
"
<span
class=
'pyx_c_api'
>
__Pyx_AddTraceback
</span>
(\"_cython_magic_52e640a266384bf0478136fe7ab4a75d.cprimes\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
" __pyx_r = NULL;\n",
" __pyx_L0:;\n",
"
<span
class=
'pyx_macro_api'
>
__Pyx_XDECREF
</span>
(__pyx_v_result);\n",
"
<span
class=
'refnanny'
>
__Pyx_XGIVEREF
</span>
(__pyx_r);\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannyFinishContext
</span>
();\n",
" return __pyx_r;\n",
"}\n",
"/* … */\n",
" __pyx_tuple_ =
<span
class=
'py_c_api'
>
PyTuple_Pack
</span>
(7, __pyx_n_s_kmax, __pyx_n_s_kmax, __pyx_n_s_p, __pyx_n_s_result, __pyx_n_s_k, __pyx_n_s_n, __pyx_n_s_i); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 1, __pyx_L1_error)\n",
"
<span
class=
'refnanny'
>
__Pyx_GOTREF
</span>
(__pyx_tuple_);\n",
"
<span
class=
'refnanny'
>
__Pyx_GIVEREF
</span>
(__pyx_tuple_);\n",
"/* … */\n",
" __pyx_t_1 = PyCFunction_NewEx(
&
__pyx_mdef_46_cython_magic_52e640a266384bf0478136fe7ab4a75d_1cprimes, NULL, __pyx_n_s_cython_magic_52e640a266384bf047); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error)\n",
"
<span
class=
'refnanny'
>
__Pyx_GOTREF
</span>
(__pyx_t_1);\n",
" if (
<span
class=
'py_c_api'
>
PyDict_SetItem
</span>
(__pyx_d, __pyx_n_s_cprimes, __pyx_t_1)
<
0) __PYX_ERR(0, 1, __pyx_L1_error)\n",
"
<span
class=
'pyx_macro_api'
>
__Pyx_DECREF
</span>
(__pyx_t_1); __pyx_t_1 = 0;\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
>
 
<span
class=
\"\"
>
02
</span>
:
<span
class=
\"k\"
>
cdef
</span>
<span
class=
\"kt\"
>
int
</span>
[1000]
<span
class=
\"nf\"
>
p
</span></pre>
\n",
"
<pre
class=
\"cython
line
score-5
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
03
</span>
:
<span
class=
\"n\"
>
result
</span>
<span
class=
\"o\"
>
=
</span>
<span
class=
\"p\"
>
[]
</span></pre>
\n",
"
<pre
class=
'cython code score-5 '
>
__pyx_t_1 =
<span
class=
'py_c_api'
>
PyList_New
</span>
(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 3, __pyx_L1_error)\n",
"
<span
class=
'refnanny'
>
__Pyx_GOTREF
</span>
(__pyx_t_1);\n",
" __pyx_v_result = ((PyObject*)__pyx_t_1);\n",
" __pyx_t_1 = 0;\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
04
</span>
:
<span
class=
\"k\"
>
if
</span>
<span
class=
\"n\"
>
kmax
</span>
<span
class=
\"o\"
>
>
</span>
<span
class=
\"mf\"
>
1000
</span><span
class=
\"p\"
>
:
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
__pyx_t_2 = ((__pyx_v_kmax
>
0x3E8) != 0);\n",
" if (__pyx_t_2) {\n",
"/* … */\n",
" }\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
05
</span>
:
<span
class=
\"n\"
>
kmax
</span>
<span
class=
\"o\"
>
=
</span>
<span
class=
\"mf\"
>
1000
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
__pyx_v_kmax = 0x3E8;\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
>
 
<span
class=
\"\"
>
06
</span>
:
<span
class=
\"k\"
>
cdef
</span>
<span
class=
\"kt\"
>
int
</span>
<span
class=
\"nf\"
>
k
</span><span
class=
\"p\"
>
,
</span>
<span
class=
\"nf\"
>
n
</span><span
class=
\"p\"
>
,
</span>
<span
class=
\"nf\"
>
i
</span></pre>
\n",
"
<pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
07
</span>
:
<span
class=
\"n\"
>
k
</span>
<span
class=
\"o\"
>
=
</span>
<span
class=
\"mf\"
>
0
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
__pyx_v_k = 0;\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
08
</span>
:
<span
class=
\"n\"
>
n
</span>
<span
class=
\"o\"
>
=
</span>
<span
class=
\"mf\"
>
2
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
__pyx_v_n = 2;\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
09
</span>
:
<span
class=
\"k\"
>
while
</span>
<span
class=
\"n\"
>
k
</span>
<span
class=
\"o\"
>
<
</span>
<span
class=
\"n\"
>
kmax
</span><span
class=
\"p\"
>
:
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
while (1) {\n",
" __pyx_t_2 = ((__pyx_v_k
<
__pyx_v_kmax) != 0);\n",
" if (!__pyx_t_2) break;\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
10
</span>
:
<span
class=
\"n\"
>
i
</span>
<span
class=
\"o\"
>
=
</span>
<span
class=
\"mf\"
>
0
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
__pyx_v_i = 0;\n",
"
</pre><pre
class=
\"cython
line
score-5
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
11
</span>
:
<span
class=
\"k\"
>
while
</span>
<span
class=
\"n\"
>
i
</span>
<span
class=
\"o\"
>
<
</span>
<span
class=
\"n\"
>
k
</span>
<span
class=
\"ow\"
>
and
</span>
<span
class=
\"n\"
>
n
</span>
<span
class=
\"o\"
>
%
</span>
<span
class=
\"n\"
>
p
</span><span
class=
\"p\"
>
[
</span><span
class=
\"n\"
>
i
</span><span
class=
\"p\"
>
]
</span>
<span
class=
\"o\"
>
!=
</span>
<span
class=
\"mf\"
>
0
</span><span
class=
\"p\"
>
:
</span></pre>
\n",
"
<pre
class=
'cython code score-5 '
>
while (1) {\n",
" __pyx_t_3 = ((__pyx_v_i
<
__pyx_v_k) != 0);\n",
" if (__pyx_t_3) {\n",
" } else {\n",
" __pyx_t_2 = __pyx_t_3;\n",
" goto __pyx_L8_bool_binop_done;\n",
" }\n",
" if (unlikely((__pyx_v_p[__pyx_v_i]) == 0)) {\n",
"
<span
class=
'py_c_api'
>
PyErr_SetString
</span>
(PyExc_ZeroDivisionError, \"integer division or modulo by zero\");\n",
" __PYX_ERR(0, 11, __pyx_L1_error)\n",
" }\n",
" __pyx_t_3 = ((__Pyx_mod_int(__pyx_v_n, (__pyx_v_p[__pyx_v_i])) != 0) != 0);\n",
" __pyx_t_2 = __pyx_t_3;\n",
" __pyx_L8_bool_binop_done:;\n",
" if (!__pyx_t_2) break;\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
12
</span>
:
<span
class=
\"n\"
>
i
</span>
<span
class=
\"o\"
>
=
</span>
<span
class=
\"n\"
>
i
</span>
<span
class=
\"o\"
>
+
</span>
<span
class=
\"mf\"
>
1
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
__pyx_v_i = (__pyx_v_i + 1);\n",
" }\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
13
</span>
:
<span
class=
\"k\"
>
if
</span>
<span
class=
\"n\"
>
i
</span>
<span
class=
\"o\"
>
==
</span>
<span
class=
\"n\"
>
k
</span><span
class=
\"p\"
>
:
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
__pyx_t_2 = ((__pyx_v_i == __pyx_v_k) != 0);\n",
" if (__pyx_t_2) {\n",
"/* … */\n",
" }\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
14
</span>
:
<span
class=
\"n\"
>
p
</span><span
class=
\"p\"
>
[
</span><span
class=
\"n\"
>
k
</span><span
class=
\"p\"
>
]
</span>
<span
class=
\"o\"
>
=
</span>
<span
class=
\"n\"
>
n
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
(__pyx_v_p[__pyx_v_k]) = __pyx_v_n;\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
15
</span>
:
<span
class=
\"n\"
>
k
</span>
<span
class=
\"o\"
>
=
</span>
<span
class=
\"n\"
>
k
</span>
<span
class=
\"o\"
>
+
</span>
<span
class=
\"mf\"
>
1
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
__pyx_v_k = (__pyx_v_k + 1);\n",
"
</pre><pre
class=
\"cython
line
score-5
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
16
</span>
:
<span
class=
\"n\"
>
result
</span><span
class=
\"o\"
>
.
</span><span
class=
\"n\"
>
append
</span><span
class=
\"p\"
>
(
</span><span
class=
\"n\"
>
n
</span><span
class=
\"p\"
>
)
</span></pre>
\n",
"
<pre
class=
'cython code score-5 '
>
__pyx_t_1 =
<span
class=
'pyx_c_api'
>
__Pyx_PyInt_From_int
</span>
(__pyx_v_n); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 16, __pyx_L1_error)\n",
"
<span
class=
'refnanny'
>
__Pyx_GOTREF
</span>
(__pyx_t_1);\n",
" __pyx_t_4 =
<span
class=
'pyx_c_api'
>
__Pyx_PyList_Append
</span>
(__pyx_v_result, __pyx_t_1); if (unlikely(__pyx_t_4 == -1)) __PYX_ERR(0, 16, __pyx_L1_error)\n",
"
<span
class=
'pyx_macro_api'
>
__Pyx_DECREF
</span>
(__pyx_t_1); __pyx_t_1 = 0;\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
17
</span>
:
<span
class=
\"n\"
>
n
</span>
<span
class=
\"o\"
>
=
</span>
<span
class=
\"n\"
>
n
</span>
<span
class=
\"o\"
>
+
</span>
<span
class=
\"mf\"
>
1
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
__pyx_v_n = (__pyx_v_n + 1);\n",
" }\n",
"
</pre><pre
class=
\"cython
line
score-2
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
18
</span>
:
<span
class=
\"k\"
>
return
</span>
<span
class=
\"n\"
>
result
</span></pre>
\n",
"
<pre
class=
'cython code score-2 '
>
<span
class=
'pyx_macro_api'
>
__Pyx_XDECREF
</span>
(__pyx_r);\n",
"
<span
class=
'pyx_macro_api'
>
__Pyx_INCREF
</span>
(__pyx_v_result);\n",
" __pyx_r = __pyx_v_result;\n",
" goto __pyx_L0;\n",
"
</pre></div></body></html>
"
],
"text/plain": [
"
<IPython.core.display.HTML
object
>
"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"%%cython -a\n",
"def cprimes(
int
kmax):\n",
"
cdef int[1000] p
\n",
"def cprimes(kmax):\n",
"
p = {}
\n",
" result = []\n",
" if kmax > 1000:\n",
" kmax = 1000\n",
" cdef int k, n, i\n",
" k = 0\n",
" n = 2\n",
" while k < kmax:\n",
...
...
@@ -1273,19 +421,9 @@
},
{
"cell_type": "code",
"
execution_count
"
:
92
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"[2,
3,
5,
7,
11,
13,
17,
19,
23,
29]\
n
",
"
CPU
times:
user
2.2
ms
,
sys:
10
µs
,
total:
2.21
ms
\
n
",
"
Wall
time:
2.22
ms
\
n
"
]
}
],
"outputs": [],
"source": [
"print(cprimes(10))\n",
"%time _ = cprimes(10000)"
...
...
@@ -1300,535 +438,41 @@
},
{
"cell_type": "code",
"
execution_count
"
:
47
,
"execution_count":
null
,
"metadata": {},
"outputs": [],
"source": [
"
#
%%
cython
-a
\
n
",
"
def
fib
(
n
)
:
\
n
",
"%%cython -a\n",
"def
c
fib(n):\n",
" if n <= 1:\n",
" return 1\n",
" else:\n",
"
return
fib
(
n-2
)
+
fib
(
n-1
)"
" return
c
fib(n-2) +
c
fib(n-1)"
]
},
{
"cell_type": "code",
"
execution_count
"
:
48
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
1.48
s
,
sys:
1.79
ms
,
total:
1.48
s
\
n
",
"
Wall
time:
1.48
s
\
n
"
]
},
{
"
data
"
:
{
"
text
/
plain
"
:
[
"5702887"
]
},
"
execution_count
"
:
48,
"
metadata
"
:
{},
"
output_type
"
:
"
execute_result
"
}
],
"outputs": [],
"source": [
"%
time
fib
(33)"
"%time
c
fib(33)"
]
},
{
"cell_type": "code",
"
execution_count
"
:
49
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
27.5
ms
,
sys:
73
µs
,
total:
27.5
ms
\
n
",
"
Wall
time:
27.5
ms
\
n
"
]
},
{
"
data
"
:
{
"
text
/
plain
"
:
[
"5702887"
]
},
"
execution_count
"
:
49,
"
metadata
"
:
{},
"
output_type
"
:
"
execute_result
"
}
],
"outputs": [],
"source": [
"%time fib_jit(33)"
]
},
{
"cell_type": "code",
"
execution_count
"
:
56
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
data
"
:
{
"
text
/
html
"
:
[
"<!
DOCTYPE
html
>
\n",
"
<!-- Generated by Cython 0.26 -->
\n",
"
<html>
\n",
"
<head>
\n",
"
<meta
http-equiv=
\"Content-Type\"
content=
\"text/html;
charset=
utf-8\"
/>
\n",
"
<title>
Cython: _cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b.pyx
</title>
\n",
"
<style
type=
\"text/css\"
>
\
n
",
"
\
n
",
"
body
.cython
{
font-family
:
courier
;
font-size
:
12
;
}
\
n
",
"
\
n
",
"
.cython.tag
{
}
\
n
",
"
.cython.line
{
margin
:
0em
}
\
n
",
"
.cython.code
{
font-size
:
9
;
color
:
#444444
;
display
:
none
;
margin
:
0px
0px
0px
8px
;
border-left
:
8px
none
;
}
\
n
",
"
\
n
",
"
.cython.line
.run
{
background-color
:
#B0FFB0
;
}
\
n
",
"
.cython.line
.mis
{
background-color
:
#FFB0B0
;
}
\
n
",
"
.cython.code.run
{
border-left
:
8px
solid
#B0FFB0
;
}
\
n
",
"
.cython.code.mis
{
border-left
:
8px
solid
#FFB0B0
;
}
\
n
",
"
\
n
",
"
.cython.code
.py_c_api
{
color
:
red
;
}
\
n
",
"
.cython.code
.py_macro_api
{
color
:
#FF7000
;
}
\
n
",
"
.cython.code
.pyx_c_api
{
color
:
#FF3000
;
}
\
n
",
"
.cython.code
.pyx_macro_api
{
color
:
#FF7000
;
}
\
n
",
"
.cython.code
.refnanny
{
color
:
#FFA000
;
}
\
n
",
"
.cython.code
.trace
{
color
:
#FFA000
;
}
\
n
",
"
.cython.code
.error_goto
{
color
:
#FFA000
;
}
\
n
",
"
\
n
",
"
.cython.code
.coerce
{
color
:
#008000
;
border
:
1px
dotted
#008000
}
\
n
",
"
.cython.code
.py_attr
{
color
:
#FF0000
;
font-weight
:
bold
;
}
\
n
",
"
.cython.code
.c_attr
{
color
:
#0000FF
;
}
\
n
",
"
.cython.code
.py_call
{
color
:
#FF0000
;
font-weight
:
bold
;
}
\
n
",
"
.cython.code
.c_call
{
color
:
#0000FF
;
}
\
n
",
"
\
n
",
"
.cython.score-0
{
background-color
:
#FFFFff
;}
\
n
",
"
.cython.score-1
{
background-color
:
#FFFFe7
;}
\
n
",
"
.cython.score-2
{
background-color
:
#FFFFd4
;}
\
n
",
"
.cython.score-3
{
background-color
:
#FFFFc4
;}
\
n
",
"
.cython.score-4
{
background-color
:
#FFFFb6
;}
\
n
",
"
.cython.score-5
{
background-color
:
#FFFFaa
;}
\
n
",
"
.cython.score-6
{
background-color
:
#FFFF9f
;}
\
n
",
"
.cython.score-7
{
background-color
:
#FFFF96
;}
\
n
",
"
.cython.score-8
{
background-color
:
#FFFF8d
;}
\
n
",
"
.cython.score-9
{
background-color
:
#FFFF86
;}
\
n
",
"
.cython.score-10
{
background-color
:
#FFFF7f
;}
\
n
",
"
.cython.score-11
{
background-color
:
#FFFF79
;}
\
n
",
"
.cython.score-12
{
background-color
:
#FFFF73
;}
\
n
",
"
.cython.score-13
{
background-color
:
#FFFF6e
;}
\
n
",
"
.cython.score-14
{
background-color
:
#FFFF6a
;}
\
n
",
"
.cython.score-15
{
background-color
:
#FFFF66
;}
\
n
",
"
.cython.score-16
{
background-color
:
#FFFF62
;}
\
n
",
"
.cython.score-17
{
background-color
:
#FFFF5e
;}
\
n
",
"
.cython.score-18
{
background-color
:
#FFFF5b
;}
\
n
",
"
.cython.score-19
{
background-color
:
#FFFF57
;}
\
n
",
"
.cython.score-20
{
background-color
:
#FFFF55
;}
\
n
",
"
.cython.score-21
{
background-color
:
#FFFF52
;}
\
n
",
"
.cython.score-22
{
background-color
:
#FFFF4f
;}
\
n
",
"
.cython.score-23
{
background-color
:
#FFFF4d
;}
\
n
",
"
.cython.score-24
{
background-color
:
#FFFF4b
;}
\
n
",
"
.cython.score-25
{
background-color
:
#FFFF48
;}
\
n
",
"
.cython.score-26
{
background-color
:
#FFFF46
;}
\
n
",
"
.cython.score-27
{
background-color
:
#FFFF44
;}
\
n
",
"
.cython.score-28
{
background-color
:
#FFFF43
;}
\
n
",
"
.cython.score-29
{
background-color
:
#FFFF41
;}
\
n
",
"
.cython.score-30
{
background-color
:
#FFFF3f
;}
\
n
",
"
.cython.score-31
{
background-color
:
#FFFF3e
;}
\
n
",
"
.cython.score-32
{
background-color
:
#FFFF3c
;}
\
n
",
"
.cython.score-33
{
background-color
:
#FFFF3b
;}
\
n
",
"
.cython.score-34
{
background-color
:
#FFFF39
;}
\
n
",
"
.cython.score-35
{
background-color
:
#FFFF38
;}
\
n
",
"
.cython.score-36
{
background-color
:
#FFFF37
;}
\
n
",
"
.cython.score-37
{
background-color
:
#FFFF36
;}
\
n
",
"
.cython.score-38
{
background-color
:
#FFFF35
;}
\
n
",
"
.cython.score-39
{
background-color
:
#FFFF34
;}
\
n
",
"
.cython.score-40
{
background-color
:
#FFFF33
;}
\
n
",
"
.cython.score-41
{
background-color
:
#FFFF32
;}
\
n
",
"
.cython.score-42
{
background-color
:
#FFFF31
;}
\
n
",
"
.cython.score-43
{
background-color
:
#FFFF30
;}
\
n
",
"
.cython.score-44
{
background-color
:
#FFFF2f
;}
\
n
",
"
.cython.score-45
{
background-color
:
#FFFF2e
;}
\
n
",
"
.cython.score-46
{
background-color
:
#FFFF2d
;}
\
n
",
"
.cython.score-47
{
background-color
:
#FFFF2c
;}
\
n
",
"
.cython.score-48
{
background-color
:
#FFFF2b
;}
\
n
",
"
.cython.score-49
{
background-color
:
#FFFF2b
;}
\
n
",
"
.cython.score-50
{
background-color
:
#FFFF2a
;}
\
n
",
"
.cython.score-51
{
background-color
:
#FFFF29
;}
\
n
",
"
.cython.score-52
{
background-color
:
#FFFF29
;}
\
n
",
"
.cython.score-53
{
background-color
:
#FFFF28
;}
\
n
",
"
.cython.score-54
{
background-color
:
#FFFF27
;}
\
n
",
"
.cython.score-55
{
background-color
:
#FFFF27
;}
\
n
",
"
.cython.score-56
{
background-color
:
#FFFF26
;}
\
n
",
"
.cython.score-57
{
background-color
:
#FFFF26
;}
\
n
",
"
.cython.score-58
{
background-color
:
#FFFF25
;}
\
n
",
"
.cython.score-59
{
background-color
:
#FFFF24
;}
\
n
",
"
.cython.score-60
{
background-color
:
#FFFF24
;}
\
n
",
"
.cython.score-61
{
background-color
:
#FFFF23
;}
\
n
",
"
.cython.score-62
{
background-color
:
#FFFF23
;}
\
n
",
"
.cython.score-63
{
background-color
:
#FFFF22
;}
\
n
",
"
.cython.score-64
{
background-color
:
#FFFF22
;}
\
n
",
"
.cython.score-65
{
background-color
:
#FFFF22
;}
\
n
",
"
.cython.score-66
{
background-color
:
#FFFF21
;}
\
n
",
"
.cython.score-67
{
background-color
:
#FFFF21
;}
\
n
",
"
.cython.score-68
{
background-color
:
#FFFF20
;}
\
n
",
"
.cython.score-69
{
background-color
:
#FFFF20
;}
\
n
",
"
.cython.score-70
{
background-color
:
#FFFF1f
;}
\
n
",
"
.cython.score-71
{
background-color
:
#FFFF1f
;}
\
n
",
"
.cython.score-72
{
background-color
:
#FFFF1f
;}
\
n
",
"
.cython.score-73
{
background-color
:
#FFFF1e
;}
\
n
",
"
.cython.score-74
{
background-color
:
#FFFF1e
;}
\
n
",
"
.cython.score-75
{
background-color
:
#FFFF1e
;}
\
n
",
"
.cython.score-76
{
background-color
:
#FFFF1d
;}
\
n
",
"
.cython.score-77
{
background-color
:
#FFFF1d
;}
\
n
",
"
.cython.score-78
{
background-color
:
#FFFF1c
;}
\
n
",
"
.cython.score-79
{
background-color
:
#FFFF1c
;}
\
n
",
"
.cython.score-80
{
background-color
:
#FFFF1c
;}
\
n
",
"
.cython.score-81
{
background-color
:
#FFFF1c
;}
\
n
",
"
.cython.score-82
{
background-color
:
#FFFF1b
;}
\
n
",
"
.cython.score-83
{
background-color
:
#FFFF1b
;}
\
n
",
"
.cython.score-84
{
background-color
:
#FFFF1b
;}
\
n
",
"
.cython.score-85
{
background-color
:
#FFFF1a
;}
\
n
",
"
.cython.score-86
{
background-color
:
#FFFF1a
;}
\
n
",
"
.cython.score-87
{
background-color
:
#FFFF1a
;}
\
n
",
"
.cython.score-88
{
background-color
:
#FFFF1a
;}
\
n
",
"
.cython.score-89
{
background-color
:
#FFFF19
;}
\
n
",
"
.cython.score-90
{
background-color
:
#FFFF19
;}
\
n
",
"
.cython.score-91
{
background-color
:
#FFFF19
;}
\
n
",
"
.cython.score-92
{
background-color
:
#FFFF19
;}
\
n
",
"
.cython.score-93
{
background-color
:
#FFFF18
;}
\
n
",
"
.cython.score-94
{
background-color
:
#FFFF18
;}
\
n
",
"
.cython.score-95
{
background-color
:
#FFFF18
;}
\
n
",
"
.cython.score-96
{
background-color
:
#FFFF18
;}
\
n
",
"
.cython.score-97
{
background-color
:
#FFFF17
;}
\
n
",
"
.cython.score-98
{
background-color
:
#FFFF17
;}
\
n
",
"
.cython.score-99
{
background-color
:
#FFFF17
;}
\
n
",
"
.cython.score-100
{
background-color
:
#FFFF17
;}
\
n
",
"
.cython.score-101
{
background-color
:
#FFFF16
;}
\
n
",
"
.cython.score-102
{
background-color
:
#FFFF16
;}
\
n
",
"
.cython.score-103
{
background-color
:
#FFFF16
;}
\
n
",
"
.cython.score-104
{
background-color
:
#FFFF16
;}
\
n
",
"
.cython.score-105
{
background-color
:
#FFFF16
;}
\
n
",
"
.cython.score-106
{
background-color
:
#FFFF15
;}
\
n
",
"
.cython.score-107
{
background-color
:
#FFFF15
;}
\
n
",
"
.cython.score-108
{
background-color
:
#FFFF15
;}
\
n
",
"
.cython.score-109
{
background-color
:
#FFFF15
;}
\
n
",
"
.cython.score-110
{
background-color
:
#FFFF15
;}
\
n
",
"
.cython.score-111
{
background-color
:
#FFFF15
;}
\
n
",
"
.cython.score-112
{
background-color
:
#FFFF14
;}
\
n
",
"
.cython.score-113
{
background-color
:
#FFFF14
;}
\
n
",
"
.cython.score-114
{
background-color
:
#FFFF14
;}
\
n
",
"
.cython.score-115
{
background-color
:
#FFFF14
;}
\
n
",
"
.cython.score-116
{
background-color
:
#FFFF14
;}
\
n
",
"
.cython.score-117
{
background-color
:
#FFFF14
;}
\
n
",
"
.cython.score-118
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-119
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-120
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-121
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-122
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-123
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-124
{
background-color
:
#FFFF13
;}
\
n
",
"
.cython.score-125
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-126
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-127
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-128
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-129
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-130
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-131
{
background-color
:
#FFFF12
;}
\
n
",
"
.cython.score-132
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-133
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-134
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-135
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-136
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-137
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-138
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-139
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-140
{
background-color
:
#FFFF11
;}
\
n
",
"
.cython.score-141
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-142
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-143
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-144
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-145
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-146
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-147
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-148
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-149
{
background-color
:
#FFFF10
;}
\
n
",
"
.cython.score-150
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-151
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-152
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-153
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-154
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-155
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-156
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-157
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-158
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-159
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-160
{
background-color
:
#FFFF0f
;}
\
n
",
"
.cython.score-161
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-162
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-163
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-164
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-165
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-166
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-167
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-168
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-169
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-170
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-171
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-172
{
background-color
:
#FFFF0e
;}
\
n
",
"
.cython.score-173
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-174
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-175
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-176
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-177
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-178
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-179
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-180
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-181
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-182
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-183
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-184
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-185
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-186
{
background-color
:
#FFFF0d
;}
\
n
",
"
.cython.score-187
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-188
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-189
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-190
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-191
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-192
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-193
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-194
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-195
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-196
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-197
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-198
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-199
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-200
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-201
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-202
{
background-color
:
#FFFF0c
;}
\
n
",
"
.cython.score-203
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-204
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-205
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-206
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-207
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-208
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-209
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-210
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-211
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-212
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-213
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-214
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-215
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-216
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-217
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-218
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-219
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-220
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-221
{
background-color
:
#FFFF0b
;}
\
n
",
"
.cython.score-222
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-223
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-224
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-225
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-226
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-227
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-228
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-229
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-230
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-231
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-232
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-233
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-234
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-235
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-236
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-237
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-238
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-239
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-240
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-241
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-242
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-243
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-244
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-245
{
background-color
:
#FFFF0a
;}
\
n
",
"
.cython.score-246
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-247
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-248
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-249
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-250
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-251
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-252
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-253
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython.score-254
{
background-color
:
#FFFF09
;}
\
n
",
"
.cython
.hll
{
background-color
:
#ffffcc
}
\
n
",
"
.cython
{
background
:
#f8f8f8
;
}
\
n
",
"
.cython
.c
{
color
:
#408080
;
font-style
:
italic
}
/* Comment */
\
n
",
"
.cython
.err
{
border
:
1px
solid
#FF0000
}
/* Error */
\
n
",
"
.cython
.k
{
color
:
#008000
;
font-weight
:
bold
}
/* Keyword */
\
n
",
"
.cython
.o
{
color
:
#666666
}
/* Operator */
\
n
",
"
.cython
.ch
{
color
:
#408080
;
font-style
:
italic
}
/* Comment.Hashbang */
\
n
",
"
.cython
.cm
{
color
:
#408080
;
font-style
:
italic
}
/* Comment.Multiline */
\
n
",
"
.cython
.cp
{
color
:
#BC7A00
}
/* Comment.Preproc */
\
n
",
"
.cython
.cpf
{
color
:
#408080
;
font-style
:
italic
}
/* Comment.PreprocFile */
\
n
",
"
.cython
.c1
{
color
:
#408080
;
font-style
:
italic
}
/* Comment.Single */
\
n
",
"
.cython
.cs
{
color
:
#408080
;
font-style
:
italic
}
/* Comment.Special */
\
n
",
"
.cython
.gd
{
color
:
#A00000
}
/* Generic.Deleted */
\
n
",
"
.cython
.ge
{
font-style
:
italic
}
/* Generic.Emph */
\
n
",
"
.cython
.gr
{
color
:
#FF0000
}
/* Generic.Error */
\
n
",
"
.cython
.gh
{
color
:
#000080
;
font-weight
:
bold
}
/* Generic.Heading */
\
n
",
"
.cython
.gi
{
color
:
#00A000
}
/* Generic.Inserted */
\
n
",
"
.cython
.go
{
color
:
#888888
}
/* Generic.Output */
\
n
",
"
.cython
.gp
{
color
:
#000080
;
font-weight
:
bold
}
/* Generic.Prompt */
\
n
",
"
.cython
.gs
{
font-weight
:
bold
}
/* Generic.Strong */
\
n
",
"
.cython
.gu
{
color
:
#800080
;
font-weight
:
bold
}
/* Generic.Subheading */
\
n
",
"
.cython
.gt
{
color
:
#0044DD
}
/* Generic.Traceback */
\
n
",
"
.cython
.kc
{
color
:
#008000
;
font-weight
:
bold
}
/* Keyword.Constant */
\
n
",
"
.cython
.kd
{
color
:
#008000
;
font-weight
:
bold
}
/* Keyword.Declaration */
\
n
",
"
.cython
.kn
{
color
:
#008000
;
font-weight
:
bold
}
/* Keyword.Namespace */
\
n
",
"
.cython
.kp
{
color
:
#008000
}
/* Keyword.Pseudo */
\
n
",
"
.cython
.kr
{
color
:
#008000
;
font-weight
:
bold
}
/* Keyword.Reserved */
\
n
",
"
.cython
.kt
{
color
:
#B00040
}
/* Keyword.Type */
\
n
",
"
.cython
.m
{
color
:
#666666
}
/* Literal.Number */
\
n
",
"
.cython
.s
{
color
:
#BA2121
}
/* Literal.String */
\
n
",
"
.cython
.na
{
color
:
#7D9029
}
/* Name.Attribute */
\
n
",
"
.cython
.nb
{
color
:
#008000
}
/* Name.Builtin */
\
n
",
"
.cython
.nc
{
color
:
#0000FF
;
font-weight
:
bold
}
/* Name.Class */
\
n
",
"
.cython
.no
{
color
:
#880000
}
/* Name.Constant */
\
n
",
"
.cython
.nd
{
color
:
#AA22FF
}
/* Name.Decorator */
\
n
",
"
.cython
.ni
{
color
:
#999999
;
font-weight
:
bold
}
/* Name.Entity */
\
n
",
"
.cython
.ne
{
color
:
#D2413A
;
font-weight
:
bold
}
/* Name.Exception */
\
n
",
"
.cython
.nf
{
color
:
#0000FF
}
/* Name.Function */
\
n
",
"
.cython
.nl
{
color
:
#A0A000
}
/* Name.Label */
\
n
",
"
.cython
.nn
{
color
:
#0000FF
;
font-weight
:
bold
}
/* Name.Namespace */
\
n
",
"
.cython
.nt
{
color
:
#008000
;
font-weight
:
bold
}
/* Name.Tag */
\
n
",
"
.cython
.nv
{
color
:
#19177C
}
/* Name.Variable */
\
n
",
"
.cython
.ow
{
color
:
#AA22FF
;
font-weight
:
bold
}
/* Operator.Word */
\
n
",
"
.cython
.w
{
color
:
#bbbbbb
}
/* Text.Whitespace */
\
n
",
"
.cython
.mb
{
color
:
#666666
}
/* Literal.Number.Bin */
\
n
",
"
.cython
.mf
{
color
:
#666666
}
/* Literal.Number.Float */
\
n
",
"
.cython
.mh
{
color
:
#666666
}
/* Literal.Number.Hex */
\
n
",
"
.cython
.mi
{
color
:
#666666
}
/* Literal.Number.Integer */
\
n
",
"
.cython
.mo
{
color
:
#666666
}
/* Literal.Number.Oct */
\
n
",
"
.cython
.sa
{
color
:
#BA2121
}
/* Literal.String.Affix */
\
n
",
"
.cython
.sb
{
color
:
#BA2121
}
/* Literal.String.Backtick */
\
n
",
"
.cython
.sc
{
color
:
#BA2121
}
/* Literal.String.Char */
\
n
",
"
.cython
.dl
{
color
:
#BA2121
}
/* Literal.String.Delimiter */
\
n
",
"
.cython
.sd
{
color
:
#BA2121
;
font-style
:
italic
}
/* Literal.String.Doc */
\
n
",
"
.cython
.s2
{
color
:
#BA2121
}
/* Literal.String.Double */
\
n
",
"
.cython
.se
{
color
:
#BB6622
;
font-weight
:
bold
}
/* Literal.String.Escape */
\
n
",
"
.cython
.sh
{
color
:
#BA2121
}
/* Literal.String.Heredoc */
\
n
",
"
.cython
.si
{
color
:
#BB6688
;
font-weight
:
bold
}
/* Literal.String.Interpol */
\
n
",
"
.cython
.sx
{
color
:
#008000
}
/* Literal.String.Other */
\
n
",
"
.cython
.sr
{
color
:
#BB6688
}
/* Literal.String.Regex */
\
n
",
"
.cython
.s1
{
color
:
#BA2121
}
/* Literal.String.Single */
\
n
",
"
.cython
.ss
{
color
:
#19177C
}
/* Literal.String.Symbol */
\
n
",
"
.cython
.bp
{
color
:
#008000
}
/* Name.Builtin.Pseudo */
\
n
",
"
.cython
.fm
{
color
:
#0000FF
}
/* Name.Function.Magic */
\
n
",
"
.cython
.vc
{
color
:
#19177C
}
/* Name.Variable.Class */
\
n
",
"
.cython
.vg
{
color
:
#19177C
}
/* Name.Variable.Global */
\
n
",
"
.cython
.vi
{
color
:
#19177C
}
/* Name.Variable.Instance */
\
n
",
"
.cython
.vm
{
color
:
#19177C
}
/* Name.Variable.Magic */
\
n
",
"
.cython
.il
{
color
:
#666666
}
/* Literal.Number.Integer.Long */
\
n
",
"
</style>
\n",
"
<script>
\
n
"
,
"
function
toggleDiv
(
id
)
{
\
n
"
,
"
theDiv
=
id
.
nextElementSibling
\
n
"
,
"
if
(
theDiv
.
style
.
display
!=
'
block
'
)
theDiv
.
style
.
display
=
'
block
'
;
\
n
"
,
"
else
theDiv
.
style
.
display
=
'
none
'
;
\
n
"
,
"
}
\
n
"
,
"
</script>
\n",
"
</head>
\n",
"
<body
class=
\"cython\"
>
\n",
"
<p><span
style=
\"border-bottom:
solid
1
px
grey
;\"
>
Generated by Cython 0.26
</span></p>
\n",
"
<p>
\n",
"
<span
style=
\"background-color:
#FFFF00
\"
>
Yellow lines
</span>
hint at Python interaction.
<br
/>
\n",
" Click on a line that starts with a \"
<code>
+
</code>
\" to see the C code that Cython generated for it.\n",
"
</p>
\n",
"
<div
class=
\"cython\"
><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
1
</span>
:
<span
class=
\"k\"
>
cdef
</span>
<span
class=
\"kt\"
>
int
</span>
<span
class=
\"nf\"
>
cfib
</span><span
class=
\"p\"
>
(
</span><span
class=
\"nb\"
>
int
</span>
<span
class=
\"n\"
>
n
</span><span
class=
\"p\"
>
)
</span>
<span
class=
\"k\"
>
nogil
</span><span
class=
\"p\"
>
:
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
static int __pyx_f_46_cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b_cfib(int __pyx_v_n) {\n",
" int __pyx_r;\n",
"/* … */\n",
" /* function exit code */\n",
" __pyx_L0:;\n",
" return __pyx_r;\n",
"}\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
2
</span>
:
<span
class=
\"k\"
>
if
</span>
<span
class=
\"n\"
>
n
</span>
<span
class=
\"o\"
>
<
=
</span>
<span
class=
\"mf\"
>
1
</span><span
class=
\"p\"
>
:
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
__pyx_t_1 = ((__pyx_v_n
<
= 1) != 0);\n",
" if (__pyx_t_1) {\n",
"/* … */\n",
" }\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
3
</span>
:
<span
class=
\"k\"
>
return
</span>
<span
class=
\"mf\"
>
1
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
__pyx_r = 1;\n",
" goto __pyx_L0;\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
>
 
<span
class=
\"\"
>
4
</span>
:
<span
class=
\"k\"
>
else
</span><span
class=
\"p\"
>
:
</span></pre>
\n",
"
<pre
class=
\"cython
line
score-0
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
5
</span>
:
<span
class=
\"k\"
>
return
</span>
<span
class=
\"n\"
>
cfib
</span><span
class=
\"p\"
>
(
</span><span
class=
\"n\"
>
n
</span><span
class=
\"o\"
>
-
</span><span
class=
\"mf\"
>
2
</span><span
class=
\"p\"
>
)
</span>
<span
class=
\"o\"
>
+
</span>
<span
class=
\"n\"
>
cfib
</span><span
class=
\"p\"
>
(
</span><span
class=
\"n\"
>
n
</span><span
class=
\"o\"
>
-
</span><span
class=
\"mf\"
>
1
</span><span
class=
\"p\"
>
)
</span></pre>
\n",
"
<pre
class=
'cython code score-0 '
>
/*else*/ {\n",
" __pyx_r = (__pyx_f_46_cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b_cfib((__pyx_v_n - 2)) + __pyx_f_46_cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b_cfib((__pyx_v_n - 1)));\n",
" goto __pyx_L0;\n",
" }\n",
"
</pre><pre
class=
\"cython
line
score-0
\"
>
 
<span
class=
\"\"
>
6
</span>
:
</pre>
\n",
"
<pre
class=
\"cython
line
score-14
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
7
</span>
:
<span
class=
\"k\"
>
def
</span>
<span
class=
\"nf\"
>
fib
</span><span
class=
\"p\"
>
(
</span><span
class=
\"n\"
>
n
</span><span
class=
\"p\"
>
):
</span></pre>
\n",
"
<pre
class=
'cython code score-14 '
>
/* Python wrapper */\n",
"static PyObject *__pyx_pw_46_cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b_1fib(PyObject *__pyx_self, PyObject *__pyx_v_n); /*proto*/\n",
"static PyMethodDef __pyx_mdef_46_cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b_1fib = {\"fib\", (PyCFunction)__pyx_pw_46_cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b_1fib, METH_O, 0};\n",
"static PyObject *__pyx_pw_46_cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b_1fib(PyObject *__pyx_self, PyObject *__pyx_v_n) {\n",
" PyObject *__pyx_r = 0;\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannyDeclarations
</span>
\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannySetupContext
</span>
(\"fib (wrapper)\", 0);\n",
" __pyx_r = __pyx_pf_46_cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b_fib(__pyx_self, ((PyObject *)__pyx_v_n));\n",
"\n",
" /* function exit code */\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannyFinishContext
</span>
();\n",
" return __pyx_r;\n",
"}\n",
"\n",
"static PyObject *__pyx_pf_46_cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b_fib(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_n) {\n",
" PyObject *__pyx_r = NULL;\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannyDeclarations
</span>
\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannySetupContext
</span>
(\"fib\", 0);\n",
"/* … */\n",
" /* function exit code */\n",
" __pyx_L1_error:;\n",
"
<span
class=
'pyx_macro_api'
>
__Pyx_XDECREF
</span>
(__pyx_t_2);\n",
"
<span
class=
'pyx_c_api'
>
__Pyx_AddTraceback
</span>
(\"_cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b.fib\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n",
" __pyx_r = NULL;\n",
" __pyx_L0:;\n",
"
<span
class=
'refnanny'
>
__Pyx_XGIVEREF
</span>
(__pyx_r);\n",
"
<span
class=
'refnanny'
>
__Pyx_RefNannyFinishContext
</span>
();\n",
" return __pyx_r;\n",
"}\n",
"/* … */\n",
" __pyx_tuple_ =
<span
class=
'py_c_api'
>
PyTuple_Pack
</span>
(1, __pyx_n_s_n); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 7, __pyx_L1_error)\n",
"
<span
class=
'refnanny'
>
__Pyx_GOTREF
</span>
(__pyx_tuple_);\n",
"
<span
class=
'refnanny'
>
__Pyx_GIVEREF
</span>
(__pyx_tuple_);\n",
"/* … */\n",
" __pyx_t_1 = PyCFunction_NewEx(
&
__pyx_mdef_46_cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b_1fib, NULL, __pyx_n_s_cython_magic_ed1fcd3356d03e7c12); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 7, __pyx_L1_error)\n",
"
<span
class=
'refnanny'
>
__Pyx_GOTREF
</span>
(__pyx_t_1);\n",
" if (
<span
class=
'py_c_api'
>
PyDict_SetItem
</span>
(__pyx_d, __pyx_n_s_fib, __pyx_t_1)
<
0) __PYX_ERR(0, 7, __pyx_L1_error)\n",
"
<span
class=
'pyx_macro_api'
>
__Pyx_DECREF
</span>
(__pyx_t_1); __pyx_t_1 = 0;\n",
"
</pre><pre
class=
\"cython
line
score-10
\"
onclick=
'toggleDiv(this)'
>
+
<span
class=
\"\"
>
8
</span>
:
<span
class=
\"k\"
>
return
</span>
<span
class=
\"n\"
>
cfib
</span><span
class=
\"p\"
>
(
</span><span
class=
\"n\"
>
n
</span><span
class=
\"p\"
>
)
</span></pre>
\n",
"
<pre
class=
'cython code score-10 '
>
<span
class=
'pyx_macro_api'
>
__Pyx_XDECREF
</span>
(__pyx_r);\n",
" __pyx_t_1 =
<span
class=
'pyx_c_api'
>
__Pyx_PyInt_As_int
</span>
(__pyx_v_n); if (unlikely((__pyx_t_1 == (int)-1)
&&
<span
class=
'py_c_api'
>
PyErr_Occurred
</span>
())) __PYX_ERR(0, 8, __pyx_L1_error)\n",
" __pyx_t_2 =
<span
class=
'pyx_c_api'
>
__Pyx_PyInt_From_int
</span>
(__pyx_f_46_cython_magic_ed1fcd3356d03e7c12842ade7d34bd4b_cfib(__pyx_t_1)); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 8, __pyx_L1_error)\n",
"
<span
class=
'refnanny'
>
__Pyx_GOTREF
</span>
(__pyx_t_2);\n",
" __pyx_r = __pyx_t_2;\n",
" __pyx_t_2 = 0;\n",
" goto __pyx_L0;\n",
"
</pre></div></body></html>
"
],
"text/plain": [
"
<IPython.core.display.HTML
object
>
"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"%%cython -a\n",
"cdef int cfib(int n) nogil:\n",
...
...
@@ -1843,28 +487,9 @@
},
{
"cell_type": "code",
"
execution_count
"
:
57
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
10.2
ms
,
sys:
138
µs
,
total:
10.4
ms
\
n
",
"
Wall
time:
10.4
ms
\
n
"
]
},
{
"
data
"
:
{
"
text
/
plain
"
:
[
"5702887"
]
},
"
execution_count
"
:
57,
"
metadata
"
:
{},
"
output_type
"
:
"
execute_result
"
}
],
"outputs": [],
"source": [
"%time fib(33)"
]
...
...
@@ -1879,78 +504,36 @@
{
"cell_type": "code",
"execution_count": null,
"
metadata
"
:
{
"
collapsed
"
:
true
},
"metadata": {},
"outputs": [],
"source": [
"def root(a, b, c):\n",
" D = b ** 2 - 4 * a * c\n",
" if D < 0:\n",
"
return
NAN
,
NAN
\
n
",
"
x1 =
(-b
+
sqrt
(
D
))
/
(2
*
a
)\
n
",
"
x2 =
(-b
-
sqrt
(
D
))
/
(2
*
a
)\
n
",
"
return
x1
,
x2
"
" return np.nan, np.nan\n",
" x1 = (-b + np.sqrt(D)) / (2 * a)\n",
" x2 = (-b - np.sqrt(D)) / (2 * a)\n",
" return x1, x2\n",
"root(1, 3, 4), root(-1, 3, 4), root(1, 0, 0)"
]
},
{
"cell_type": "code",
"
execution_count
"
:
68
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
236
ms
,
sys:
53.7
ms
,
total:
290
ms
\
n
",
"
Wall
time:
288
ms
\
n
"
]
},
{
"
data
"
:
{
"
text
/
plain
"
:
[
"(
array
([
-1.72246969
,
-2.6378758
,
nan
,
...,
-2.20722389
,\
n
",
"
nan
,
-1.84415698
]),\
n
",
"
array
([2.27571702,
2.04353046,
nan
,
...,
2.62522459,
nan
,\
n
",
"
1.49223876]))"
]
},
"
execution_count
"
:
68,
"
metadata
"
:
{},
"
output_type
"
:
"
execute_result
"
}
],
"outputs": [],
"source": [
"a = np.random.randn(int(1e6))\n",
"b = np.random.randn(int(1e6))\n",
"c = np.random.randn(int(1e6))\n",
"%time np.vectorize(root)(a, b, 7)"
]
},
{
"cell_type": "code",
"
execution_count
"
:
66
,
"execution_count":
null
,
"metadata": {},
"
outputs
"
:
[
{
"
name
"
:
"
stdout
",
"
output_type
"
:
"
stream
",
"
text
"
:
[
"
CPU
times:
user
353
ms
,
sys:
57.8
ms
,
total:
411
ms
\
n
",
"
Wall
time:
410
ms
\
n
"
]
},
{
"
data
"
:
{
"
text
/
plain
"
:
[
"(
array
([
-1.72246968
,
-2.63787563
,
nan
,
...,
-2.20722406
,\
n
",
"
nan
,
-1.84415698
]),\
n
",
"
array
([2.27571708,
2.04353033,
nan
,
...,
2.62522454,
nan
,\
n
",
"
1.4922388
]))"
]
},
"
execution_count
"
:
66,
"
metadata
"
:
{},
"
output_type
"
:
"
execute_result
"
}
],
"outputs": [],
"source": [
"%time root_jit_vec(a, b, 7)"
]
...
...
@@ -1981,7 +564,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"
##
Cython
in
production
code
"
"##
More cython
"
]
},
{
...
...
@@ -1997,6 +580,7 @@
"source": [
"# Steps to optimizing code:\n",
"- Profile where the bottleneck is (%prun)\n",
" - You can install a [line profiler](https://github.com/rkern/line_profiler) (available as %lprun after installation)\n",
"- Is there a faster algorithm for the bottleneck?\n",
"- If the bottleneck is vectorized: can we optimize with numexpr?\n",
"- If the internal part of the loop can not be vectorized:\n",
...
...
%% Cell type:markdown id: tags:
# Numpy vectorizing
%% Cell type:code id: tags:
```
python
import
numpy
as
np
```
%% Cell type:code id: tags:
```
python
def
root
(
a
,
b
,
c
):
D
=
b
**
2
-
4
*
a
*
c
if
D
<
0
:
return
np
.
nan
,
np
.
nan
x1
=
(
-
b
+
np
.
sqrt
(
D
))
/
(
2
*
a
)
x2
=
(
-
b
-
np
.
sqrt
(
D
))
/
(
2
*
a
)
return
x1
,
x2
root
(
1
,
3
,
4
),
root
(
-
1
,
3
,
4
),
root
(
1
,
0
,
0
)
```
%% Output
((nan, nan), (-1.0, 4.0), (0.0, 0.0))
%% Cell type:code id: tags:
```
python
a
=
np
.
random
.
randn
(
int
(
1e6
))
b
=
np
.
random
.
randn
(
int
(
1e6
))
c
=
np
.
random
.
randn
(
int
(
1e6
))
root
(
a
,
b
,
c
)
```
%% Output
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-5d1fed3ed2df> in <module>()
2 b = np.random.randn(int(1e6))
3 c = np.random.randn(int(1e6))
----> 4 root(a, b, c)
<ipython-input-18-54b500cd66b1> in root(a, b, c)
1 def root(a, b, c):
2 D = b ** 2 - 4 * a * c
----> 3 if D < 0:
4 return np.nan, np.nan
5 x1 = (-b + np.sqrt(D)) / (2 * a)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
%% Cell type:code id: tags:
```
python
%%
time
np
.
array
([
root
(
av
,
bv
,
cv
)
for
av
,
bv
,
cv
in
zip
(
a
,
b
,
c
)])
```
%% Output
CPU times: user 3.64 s, sys: 65.9 ms, total: 3.71 s
Wall time: 3.71 s
array([[-0.55797188, 1.11121928],
[ nan, nan],
[ 0.76166857, -1.70637551],
...,
[-0.56859783, 0.98659831],
[ nan, nan],
[-0.53531175, 0.18339357]])
%% Cell type:code id: tags:
```
python
root_vec
=
np
.
vectorize
(
root
)
np
.
vectorize
?
```
%% Cell type:code id: tags:
```
python
%%
time
root_vec
(
a
,
b
,
c
)
```
%% Output
CPU times: user 1.85 s, sys: 71.9 ms, total: 1.92 s
Wall time: 1.92 s
(array([-0.55797188, nan, 0.76166857, ..., -0.56859783,
nan, -0.53531175]),
array([ 1.11121928, nan, -1.70637551, ..., 0.98659831,
nan, 0.18339357]))
%% Cell type:code id: tags:
```
python
%%
time
root_vec
(
a
,
b
,
7
)
```
%% Output
CPU times: user 1.66 s, sys: 62.7 ms, total: 1.73 s
Wall time: 1.73 s
(array([-1.72246968, -2.63787563, nan, ..., -2.20722406,
nan, -1.84415698]),
array([2.27571708, 2.04353033, nan, ..., 2.62522454, nan,
1.4922388 ]))
%% Cell type:markdown id: tags:
# Numba
%% Cell type:code id: tags:
```
python
import
numba
```
%% Cell type:code id: tags:
```
python
root_jit
=
numba
.
jit
(
root
)
```
%% Cell type:code id: tags:
```
python
%
timeit
root
(
23
,
78
,
19.0
)
```
%% Output
3.08 µs ± 192 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%% Cell type:code id: tags:
```
python
%
timeit
root_jit
(
23
,
78
,
19.0
)
```
%% Output
The slowest run took 11.09 times longer than the fastest. This could mean that an intermediate result is being cached.
786 ns ± 1.08 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
%% Cell type:code id: tags:
```
python
%%
time
root_jit
(
a
,
b
,
7
)
```
%% Output
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call last)
<timed
eval
>
in
<module>
()
SystemError: CPUDispatcher(
<function
root
at
0
x114035620
>
) returned a result with an error set
%% Cell type:code id: tags:
```
python
root_jit_vec
=
np
.
vectorize
(
root_jit
)
%
time
root_jit_vec
(
a
,
b
,
7
)
```
%% Output
CPU times: user 406 ms, sys: 57.2 ms, total: 464 ms
Wall time: 464 ms
(array([-1.72246968, -2.63787563, nan, ..., -2.20722406,
nan, -1.84415698]),
array([2.27571708, 2.04353033, nan, ..., 2.62522454, nan,
1.4922388 ]))
%% Cell type:code id: tags:
```
python
@np.vectorize
@numba.jit
def
fast_root
(
a
,
b
,
c
):
D
=
b
**
2
-
4
*
a
*
c
if
D
<
0
:
return
np
.
nan
,
np
.
nan
x1
=
(
-
b
+
np
.
sqrt
(
D
))
/
(
2
*
a
)
x2
=
(
-
b
-
np
.
sqrt
(
D
))
/
(
2
*
a
)
return
x1
,
x2
%
time
fast_root
(
a
,
b
,
7
)
```
%% Output
CPU times: user 391 ms, sys: 58.2 ms, total: 449 ms
Wall time: 448 ms
(array([-1.72246968, -2.63787563, nan, ..., -2.20722406,
nan, -1.84415698]),
array([2.27571708, 2.04353033, nan, ..., 2.62522454, nan,
1.4922388 ]))
%% Cell type:code id: tags:
```
python
@numba.vectorize
def
fast_root
(
a
,
b
,
c
):
D
=
b
**
2
-
4
*
a
*
c
if
D
<
0
:
return
np
.
nan
,
np
.
nan
x1
=
(
-
b
+
np
.
sqrt
(
D
))
/
(
2
*
a
)
x2
=
(
-
b
-
np
.
sqrt
(
D
))
/
(
2
*
a
)
return
x1
,
x2
%
time
fast_root
(
a
,
b
,
7
)
```
%% Output
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<timed eval> in <module>()
~/miniconda3/lib/python3.6/site-packages/numba/npyufunc/dufunc.py in _compile_for_args(self, *args, **kws)
166 argty = argty.dtype
167 argtys.append(argty)
--> 168 return self._compile_for_argtys(tuple(argtys))
169
170 def _compile_for_argtys(self, argtys, return_type=None):
~/miniconda3/lib/python3.6/site-packages/numba/npyufunc/dufunc.py in _compile_for_argtys(self, argtys, return_type)
188 cres, argtys, return_type)
189 dtypenums, ptr, env = ufuncbuilder._build_element_wise_ufunc_wrapper(
--> 190 cres, actual_sig)
191 self._add_loop(utils.longint(ptr), dtypenums)
192 self._keepalive.append((ptr, cres.library, env))
~/miniconda3/lib/python3.6/site-packages/numba/npyufunc/ufuncbuilder.py in _build_element_wise_ufunc_wrapper(cres, signature)
163 # Get dtypes
164 dtypenums = [as_dtype(a).num for a in signature.args]
--> 165 dtypenums.append(as_dtype(signature.return_type).num)
166 return dtypenums, ptr, env
167
~/miniconda3/lib/python3.6/site-packages/numba/numpy_support.py in as_dtype(nbtype)
134 return as_dtype(nbtype.dtype)
135 raise NotImplementedError("%r cannot be represented as a Numpy dtype"
--> 136 % (nbtype,))
137
138
NotImplementedError: (float64 x 2) cannot be represented as a Numpy dtype
%% Cell type:markdown id: tags:
# But run the profiler first
%% Cell type:code id: tags:
```
python
def
fib
(
n
):
if
n
<=
1
:
return
1
else
:
return
fib
(
n
-
2
)
+
fib
(
n
-
1
)
%
time
fib
(
33
)
```
%% Output
CPU times: user 1.56 s, sys: 3.5 ms, total: 1.56 s
Wall time: 1.56 s
5702887
%% Cell type:code id: tags:
```
python
@numba.jit
def
fib_jit
(
n
):
if
n
<=
1
:
return
1
else
:
return
fib_jit
(
n
-
2
)
+
fib_jit
(
n
-
1
)
%
time
fib_jit
(
33
)
```
%% Output
CPU times: user 63.4 ms, sys: 1.79 ms, total: 65.2 ms
Wall time: 64.2 ms
5702887
%% Cell type:code id: tags:
```
python
%
time
fib_jit
(
41
)
```
%% Output
CPU times: user 1.28 s, sys: 5.26 ms, total: 1.28 s
Wall time: 1.28 s
267914296
%% Cell type:code id: tags:
```
python
%
prun
fib
(
33
)
%
prun
fib
_jit
(
41
)
```
%% Output
%% Cell type:code id: tags:
```
python
%
prun
fib
_jit
(
41
)
%
prun
fib
(
33
)
```
%% Output
%% Cell type:code id: tags:
```
python
from
functools
import
lru_cache
@lru_cache
(
None
)
def
fib_cache
(
n
):
if
n
<=
1
:
return
1
else
:
return
fib_cache
(
n
-
2
)
+
fib_cache
(
n
-
1
)
%
time
fib_cache
(
33
)
```
%% Output
CPU times: user 20 µs, sys: 1 µs, total: 21 µs
Wall time: 24.1 µs
%% Cell type:code id: tags:
```
python
lru_cache
?
```
%% Cell type:code id: tags:
```
python
%
time
fib_cache
(
500
)
```
%% Output
CPU times: user 261 µs, sys: 1e+03 ns, total: 262 µs
Wall time: 267 µs
225591516161936330872512695036072072046011324913758190588638866418474627738686883405015987052796968498626
%% Cell type:markdown id: tags:
# Optimizing numpy algebra
%% Cell type:markdown id: tags:
Multiplying/adding arrays incurs a lot of overhead, because lots of intermediate arrays get created and discarded again
%% Cell type:code id: tags:
```
python
a
=
np
.
random
.
randn
(
int
(
1e7
))
b
=
np
.
random
.
randn
(
int
(
1e7
))
%
timeit
a
*
b
+
4.1
*
a
<
3
*
b
```
%% Output
139 ms ± 1.16 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%% Cell type:code id: tags:
```
python
import
numexpr
as
ne
%
timeit
ne
.
evaluate
(
'
a * b + 4.1 * a < 3 * b
'
)
```
%% Output
11.4 ms ± 484 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%% Cell type:markdown id: tags:
Efficiency gain reduces once the operations become more complex
%% Cell type:code id: tags:
```
python
%
timeit
a
*
np
.
sqrt
(
abs
(
b
))
<
3
*
b
c
=
np
.
random
.
randn
(
int
(
1e7
))
%
timeit
(
-
b
-
np
.
sqrt
(
b
**
2
-
4
*
a
*
c
)
)
/
(
2
*
a
)
```
%% Output
141 ms ± 1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%% Cell type:code id: tags:
```
python
%
timeit
ne
.
evaluate
(
'
a *
sqrt(
abs(b)) < 3
*
b
'
)
%
timeit
ne
.
evaluate
(
'
(-b -
sqrt(
b ** 2 - 4 * a * c) ) / (2
*
a)
'
)
```
%% Output
12.6 ms ± 986 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%% Cell type:markdown id: tags:
# Cython
%% Cell type:markdown id: tags:
http://docs.cython.org/en/latest/
Compiles most python programs to C code for extra speed (but less introspection). Also allows direct calling of C library code.
Can include python classes and much more.
%% Cell type:code id: tags:
```
python
%
load_ext
cython
```
%% Output
The cython extension is already loaded. To reload it, use:
%reload_ext cython
%% Cell type:code id: tags:
```
python
def
primes
(
kmax
):
p
=
{}
result
=
[]
if
kmax
>
1000
:
kmax
=
1000
k
=
0
n
=
2
while
k
<
kmax
:
i
=
0
while
i
<
k
and
n
%
p
[
i
]
!=
0
:
i
=
i
+
1
if
i
==
k
:
p
[
k
]
=
n
k
=
k
+
1
result
.
append
(
n
)
n
=
n
+
1
return
result
print
(
primes
(
10
))
%
time
_
=
primes
(
10000
)
```
%% Output
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
CPU times: user 67.4 ms, sys: 1.22 ms, total: 68.6 ms
Wall time: 68 ms
%% Cell type:code id: tags:
```
python
%%
cython
-
a
def
cprimes
(
int
kmax
):
cdef
int
[
1000
]
p
def
cprimes
(
kmax
):
p
=
{}
result
=
[]
if
kmax
>
1000
:
kmax
=
1000
cdef
int
k
,
n
,
i
k
=
0
n
=
2
while
k
<
kmax
:
i
=
0
while
i
<
k
and
n
%
p
[
i
]
!=
0
:
i
=
i
+
1
if
i
==
k
:
p
[
k
]
=
n
k
=
k
+
1
result
.
append
(
n
)
n
=
n
+
1
return
result
```
%% Output
<IPython.core.display.HTML object>
%% Cell type:code id: tags:
```
python
print
(
cprimes
(
10
))
%
time
_
=
cprimes
(
10000
)
```
%% Output
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
CPU times: user 2.2 ms, sys: 10 µs, total: 2.21 ms
Wall time: 2.22 ms
%% Cell type:markdown id: tags:
## Defining C-style function
%% Cell type:code id: tags:
```
python
#
%%cython -a
def
fib
(
n
):
%%
cython
-
a
def
c
fib
(
n
):
if
n
<=
1
:
return
1
else
:
return
fib
(
n
-
2
)
+
fib
(
n
-
1
)
return
c
fib
(
n
-
2
)
+
c
fib
(
n
-
1
)
```
%% Cell type:code id: tags:
```
python
%
time
fib
(
33
)
%
time
c
fib
(
33
)
```
%% Output
CPU times: user 1.48 s, sys: 1.79 ms, total: 1.48 s
Wall time: 1.48 s
5702887
%% Cell type:code id: tags:
```
python
%
time
fib_jit
(
33
)
```
%% Output
CPU times: user 27.5 ms, sys: 73 µs, total: 27.5 ms
Wall time: 27.5 ms
5702887
%% Cell type:code id: tags:
```
python
%%
cython
-
a
cdef
int
cfib
(
int
n
)
nogil
:
if
n
<=
1
:
return
1
else
:
return
cfib
(
n
-
2
)
+
cfib
(
n
-
1
)
def
fib
(
n
):
return
cfib
(
n
)
```
%% Output
<IPython.core.display.HTML object>
%% Cell type:code id: tags:
```
python
%
time
fib
(
33
)
```
%% Output
CPU times: user 10.2 ms, sys: 138 µs, total: 10.4 ms
Wall time: 10.4 ms
5702887
%% Cell type:markdown id: tags:
## Cython in production code
%% Cell type:code id: tags:
```
python
def
root
(
a
,
b
,
c
):
D
=
b
**
2
-
4
*
a
*
c
if
D
<
0
:
return
NAN
,
NAN
x1
=
(
-
b
+
sqrt
(
D
))
/
(
2
*
a
)
x2
=
(
-
b
-
sqrt
(
D
))
/
(
2
*
a
)
return
np
.
nan
,
np
.
nan
x1
=
(
-
b
+
np
.
sqrt
(
D
))
/
(
2
*
a
)
x2
=
(
-
b
-
np
.
sqrt
(
D
))
/
(
2
*
a
)
return
x1
,
x2
root
(
1
,
3
,
4
),
root
(
-
1
,
3
,
4
),
root
(
1
,
0
,
0
)
```
%% Cell type:code id: tags:
```
python
a
=
np
.
random
.
randn
(
int
(
1e6
))
b
=
np
.
random
.
randn
(
int
(
1e6
))
c
=
np
.
random
.
randn
(
int
(
1e6
))
%
time
np
.
vectorize
(
root
)(
a
,
b
,
7
)
```
%% Output
CPU times: user 236 ms, sys: 53.7 ms, total: 290 ms
Wall time: 288 ms
(array([-1.72246969, -2.6378758 , nan, ..., -2.20722389,
nan, -1.84415698]),
array([2.27571702, 2.04353046, nan, ..., 2.62522459, nan,
1.49223876]))
%% Cell type:code id: tags:
```
python
%
time
root_jit_vec
(
a
,
b
,
7
)
```
%% Output
CPU times: user 353 ms, sys: 57.8 ms, total: 411 ms
Wall time: 410 ms
(array([-1.72246968, -2.63787563, nan, ..., -2.20722406,
nan, -1.84415698]),
array([2.27571708, 2.04353033, nan, ..., 2.62522454, nan,
1.4922388 ]))
%% Cell type:code id: tags:
```
python
%%
cython
-
a
from
libc
.
math
cimport
sqrt
,
NAN
def
root
(
float
a
,
float
b
,
float
c
):
cdef
float
D
,
x1
,
x2
D
=
b
**
2
-
4
*
a
*
c
if
D
<
0
:
return
NAN
,
NAN
x1
=
(
-
b
+
sqrt
(
D
))
/
(
2
*
a
)
x2
=
(
-
b
-
sqrt
(
D
))
/
(
2
*
a
)
return
x1
,
x2
print
(
root
(
1
,
3
,
4
),
root
(
-
1
,
3
,
4
),
root
(
1
,
0
,
0
))
```
%% Cell type:markdown id: tags:
##
Cython in production code
##
More cython
%% Cell type:markdown id: tags:
http://docs.cython.org/en/latest/src/tutorial/cython_tutorial.html
%% Cell type:markdown id: tags:
# Steps to optimizing code:
-
Profile where the bottleneck is (%prun)
-
You can install a
[
line profiler
](
https://github.com/rkern/line_profiler
)
(
available
as %lprun after installation)
-
Is there a faster algorithm for the bottleneck?
-
If the bottleneck is vectorized: can we optimize with numexpr?
-
If the internal part of the loop can not be vectorized:
-
numba jit if simple enough otherwise cython
-
the loop itself can be sped up with numpy.vectorize
-
If the bottleneck gets called too often: cache the result
-
If even that fails: pycuda
-
If that is too slow: learn to optimize cuda code or find an easier problem
%% Cell type:code id: tags:
```
python
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment