You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/scenarios/speed.rst
+132Lines changed: 132 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -68,6 +68,138 @@ C Extensions
68
68
Cython
69
69
------
70
70
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
+
defprimes(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
+
defprimes(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
+
defprimes(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
+
defprimes(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:
0 commit comments