Skip to content

Commit 29a3643

Browse files
author
tommy3001
committed
Cython example added for strong typing
1 parent 09de7cb commit 29a3643

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

docs/scenarios/speed.rst

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,124 @@ 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+
With Cython you are also able to call C-functions and realize strong typing of variables and functions like float
73+
(floating point numbers) or int (integer) definition of variables. Here is an example of strong typing with Cython:
74+
75+
.. code-block:: python
76+
77+
def primes(int kmax):
78+
cdef int n, k, i
79+
cdef int p[1000]
80+
result = []
81+
if kmax > 1000:
82+
kmax = 1000
83+
k = 0
84+
n = 2
85+
while k < kmax:
86+
i = 0
87+
while i < k and n % p[i] != 0:
88+
i = i + 1
89+
if i == k:
90+
p[k] = n
91+
k = k + 1
92+
result.append(n)
93+
n = n + 1
94+
return result
95+
96+
This implementation of an algorithm to find prime numbers has some additional commands instead of the next one, which is implemented in pure Python:
97+
98+
.. code-block:: python
99+
100+
def primes( kmax):
101+
p= range(1000)
102+
result = []
103+
if kmax > 1000:
104+
kmax = 1000
105+
k = 0
106+
n = 2
107+
while k < kmax:
108+
i = 0
109+
while i < k and n % p[i] != 0:
110+
i = i + 1
111+
if i == k:
112+
p[k] = n
113+
k = k + 1
114+
result.append(n)
115+
n = n + 1
116+
return result
117+
118+
119+
The only difference between the both algorithm is this part:
120+
121+
Strong typing with Cython:
122+
123+
.. code-block:: python
124+
125+
#primes function with additional Cython code:
126+
def primes(int kmax):
127+
cdef int n, k, i
128+
cdef int p[1000]
129+
result = []
130+
131+
132+
Normal variable definition in Python:
133+
134+
.. code-block:: python
135+
136+
#primes in standard Python syntax:
137+
def primes( kmax):
138+
p= range(1000)
139+
result = []
140+
141+
142+
What is the difference? In the upper Cython version you can see the definitions of the variable types like in standard C.
143+
For example `cdef int n,k,i` in line 3. This additional type definition (e.g. integer) allows the Cython compiler to generate
144+
more efficient C code from this Cython code. While standard Python code is saved in `*.py` files, the Cython code is saved in `*.pyx` files.
145+
146+
And what is with the speed? So lets try it!
147+
148+
.. code-block:: python
149+
150+
import time
151+
#activate pyx compiler
152+
import pyximport; pyximport.install()
153+
#primes implemented with Cython
154+
import primesCy
155+
#primes implemented with Python
156+
import primes
157+
158+
print "Cython:"
159+
t1= time.time()
160+
print primesCy.primes(500)
161+
t2= time.time()
162+
print "Cython time: %s" %(t2-t1)
163+
print ""
164+
print "Python"
165+
t1= time.time()
166+
print primes.primes(500)
167+
t2= time.time()
168+
print "Python time: %s" %(t2-t1)
169+
170+
171+
Where is the magic? Here it is:
172+
173+
.. code-block:: python
174+
175+
import pyximport; pyximport.install()
176+
177+
178+
With the module `pyximport` you are able to import Cython `*.pyx` files, in this case `primesCy.pyx`, with the Cython
179+
version of the primes function.
180+
The `pyximport.install()` command allows the Python interpreter to start the Cython compiler directly to generate C-code,
181+
which is automatically compiled to a `*.so` C-library. ... and Cython is able to import this library for you in your Python-code.
182+
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.
183+
184+
Here is the output of an embedded `ARM beaglebone <http://beagleboard.org/Products/BeagleBone>`_ machine:
185+
186+
Cython time: 0.0196 seconds
187+
188+
Python time: 0.3302 seconds
71189

72190
Pyrex
73191
-----

0 commit comments

Comments
 (0)