Content deleted Content added
m Undid revision 792721227 by Special:Contributions/2600:6C58:627F:E8CA:81C8:2AF8:BECD:F3D4 |
the previous code didn't work because %load_ext Cython needs to be in a separate istruction/cell and %%cython need to be the first instruction of a new cell |
||
Line 129:
== Using in Jupyter notebook ==
A more straightforward way to start with Cython is through command-line [[IPython]] (or through in-browser python console called Jupyter [[notebook interface|notebook
<source lang="Python">
In [1]: %load_ext Cython▼
def f(n):▼
In [2]: %%cython▼
a = 0▼
cdef int a
for i in range(n):▼
return a▼
a += i▼
return a
▲ def f(n):
for i in range(n):
a += i
▲ return a
In [3]: %timeit f(1000000)▼
42.7 ms ± 783 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [4]: %timeit g(1000000)▼
74 µs ± 16.6 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
▲%load_ext Cython
▲%%cython
▲ cdef int a = 0, i
▲ for i in range(n):
▲ a += i
▲ return a
▲%timeit f(1000000)
▲%timeit g(1000000)
</source>
which gives a
== Uses ==
|