Skip to content

Commit 9427b02

Browse files
committed
Merge pull request realpython#370 from tommy3001/master
Cython description and code example added
2 parents f527049 + b8f9923 commit 9427b02

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

docs/scenarios/speed.rst

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,138 @@ C Extensions
6868
Cython
6969
------
7070

71+
With `Cython <http://cython.org/>`_ you are able to write C and C++ modules for Python. It implements a superset of the Python language.
72+
You are also able to call C-functions and realize declaration of variables and functions like in C. Here is an example:
73+
74+
.. code-block:: python
75+
76+
def primes(int kmax):
77+
"""Calculation of prime numbers with additional
78+
Cython keywords"""
79+
80+
cdef int n, k, i
81+
cdef int p[1000]
82+
result = []
83+
if kmax > 1000:
84+
kmax = 1000
85+
k = 0
86+
n = 2
87+
while k < kmax:
88+
i = 0
89+
while i < k and n % p[i] != 0:
90+
i = i + 1
91+
if i == k:
92+
p[k] = n
93+
k = k + 1
94+
result.append(n)
95+
n = n + 1
96+
return result
97+
98+
99+
This implementation of an algorithm to find prime numbers has some additional keywords instead of the next one, which is implemented in pure Python:
100+
101+
.. code-block:: python
102+
103+
104+
def primes(kmax):
105+
"""Calculation of prime numbers in standard Python syntax"""
106+
107+
p= range(1000)
108+
result = []
109+
if kmax > 1000:
110+
kmax = 1000
111+
k = 0
112+
n = 2
113+
while k < kmax:
114+
i = 0
115+
while i < k and n % p[i] != 0:
116+
i = i + 1
117+
if i == k:
118+
p[k] = n
119+
k = k + 1
120+
result.append(n)
121+
n = n + 1
122+
return result
123+
124+
125+
126+
The only difference between the both algorithm is this part:
127+
128+
129+
.. code-block:: python
130+
131+
def primes(int kmax):
132+
"""Calculation of prime numbers with additional
133+
Cython keywords"""
134+
135+
cdef int n, k, i
136+
cdef int p[1000]
137+
result = []
138+
139+
140+
.. code-block:: python
141+
142+
def primes(kmax):
143+
"""Calculation of prime numbers in standard Python syntax"""
144+
145+
p= range(1000)
146+
result = []
147+
148+
What is the difference? In the upper Cython version you can see the declaration of the variable types and the integer array
149+
in a similar way like in standard C. For example `cdef int n,k,i` in line 3. This additional type declaration (e.g. integer)
150+
allows the Cython compiler to generate more efficient C code from the second code. While standard Python code is saved in `*.py` files,
151+
Cython code is saved in `*.pyx` files.
152+
153+
And what is with the speed? So lets try it!
154+
155+
.. code-block:: python
156+
157+
import time
158+
#activate pyx compiler
159+
import pyximport
160+
pyximport.install()
161+
#primes implemented with Cython
162+
import primesCy
163+
#primes implemented with Python
164+
import primes
165+
166+
print "Cython:"
167+
t1= time.time()
168+
print primesCy.primes(500)
169+
t2= time.time()
170+
print "Cython time: %s" %(t2-t1)
171+
print ""
172+
print "Python"
173+
t1= time.time()
174+
print primes.primes(500)
175+
t2= time.time()
176+
print "Python time: %s" %(t2-t1)
177+
178+
179+
These both lines need a remark:
180+
181+
.. code-block:: python
182+
183+
import pyximport
184+
pyximport.install()
185+
186+
187+
The `pyximport` module allows you to import `pyx` files (e.g., `primesCy.pyx`) with the Cython-compiled version of the `primes` function.
188+
The `pyximport.install()` command allows the Python interpreter to start the Cython compiler directly to generate C-code,
189+
which is automatically compiled to a `*.so` C-library. Cython is able to import this library for you in your Python-code.
190+
Very easy and very efficient. With the `time.time()` function you are able to compare the time between this 2 different calls to find 500 prime numbers.
191+
192+
On a standard notebook (dualcore AMD E-450 1,6 GHz) the measured values are:
193+
194+
Cython time: 0.0054 seconds
195+
196+
Python time: 0.0566 seconds
197+
198+
And here the output of an embedded `ARM beaglebone <http://beagleboard.org/Products/BeagleBone>`_ machine:
199+
200+
Cython time: 0.0196 seconds
201+
202+
Python time: 0.3302 seconds
71203

72204
Pyrex
73205
-----

0 commit comments

Comments
 (0)