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
+26-19Lines changed: 26 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -68,10 +68,10 @@ C Extensions
68
68
Cython
69
69
------
70
70
71
-
`Cython <http://cython.org/>`_ implements a superset of the Python language
72
-
with which you are able to write C and C++ modules for Python. Cython also
73
-
allows you to call functions from compiled C libraries. Using Cython allows
74
-
you to take advantage of Python's strong typing of variables and operations.
71
+
`Cython <http://cython.org/>`_ implements a superset of the Python language
72
+
with which you are able to write C and C++ modules for Python. Cython also
73
+
allows you to call functions from compiled C libraries. Using Cython allows
74
+
you to take advantage of Python's strong typing of variables and operations.
75
75
76
76
Here's an example of strong typing with Cython:
77
77
@@ -100,11 +100,11 @@ Here's an example of strong typing with Cython:
100
100
return result
101
101
102
102
103
-
This implementation of an algorithm to find prime numbers has some additional keywords instead of the next one, which is implemented in pure Python:
103
+
This implementation of an algorithm to find prime numbers has some additional
104
+
keywords compared to the next one, which is implemented in pure Python:
104
105
105
106
.. code-block:: python
106
107
107
-
108
108
defprimes(kmax):
109
109
"""Calculation of prime numbers in standard Python syntax"""
110
110
@@ -125,7 +125,7 @@ This implementation of an algorithm to find prime numbers has some additional ke
125
125
n = n +1
126
126
return result
127
127
128
-
Notice that in the Cython version you declare integers and integer arrays for
128
+
Notice that in the Cython version you declare integers and integer arrays
129
129
to be compiled into C types while also creating a Python list:
130
130
131
131
@@ -148,12 +148,14 @@ to be compiled into C types while also creating a Python list:
148
148
p=range(1000)
149
149
result = []
150
150
151
-
What is the difference? In the upper Cython version you can see the declaration of the variable types and the integer array
152
-
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)
153
-
allows the Cython compiler to generate more efficient C code from the second code. While standard Python code is saved in :file:`*.py` files,
154
-
Cython code is saved in :file:`*.pyx` files.
151
+
What is the difference? In the upper Cython version you can see the
152
+
declaration of the variable types and the integer array in a similar way as
153
+
in standard C. For example `cdef int n,k,i` in line 3. This additional type
154
+
declaration (i.e. integer) allows the Cython compiler to generate more
155
+
efficient C code from the second version. While standard Python code is saved
156
+
in :file:`*.py` files, Cython code is saved in :file:`*.pyx` files.
155
157
156
-
And what is with the speed? So let's try it!
158
+
What's the difference in speed? Let's try it!
157
159
158
160
.. code-block:: python
159
161
@@ -179,19 +181,23 @@ And what is with the speed? So let's try it!
179
181
print"Python time: %s"%(t2-t1)
180
182
181
183
182
-
These both lines need a remark:
184
+
These lines both need a remark:
183
185
184
186
.. code-block:: python
185
187
186
188
import pyximport
187
189
pyximport.install()
188
190
189
191
190
-
The `pyximport` module allows you to import :file:`*.pyx` files (e.g., :file:`primesCy.pyx`) with the Cython-compiled version of the `primes` function.
191
-
The `pyximport.install()` command allows the Python interpreter to start the Cython compiler directly to generate C-code,
192
-
which is automatically compiled to a :file:`*.so` C-library. Cython is able to import this library for you in your Python-code.
193
-
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.
194
-
On a standard notebook (dual core AMD E-450 1.6 GHz), the measured values are:
192
+
The `pyximport` module allows you to import :file:`*.pyx` files (e.g.,
193
+
:file:`primesCy.pyx`) with the Cython-compiled version of the `primes`
194
+
function. The `pyximport.install()` command allows the Python interpreter to
195
+
start the Cython compiler directly to generate C-code, which is automatically
196
+
compiled to a :file:`*.so` C-library. Cython is then able to import this
197
+
library for you in your Python code, easily and efficiently. With the
198
+
`time.time()` function you are able to compare the time between these 2
199
+
different calls to find 500 prime numbers. On a standard notebook (dual core
200
+
AMD E-450 1.6 GHz), the measured values are:
195
201
196
202
.. code-block:: console
197
203
@@ -200,14 +206,15 @@ On a standard notebook (dual core AMD E-450 1.6 GHz), the measured values are:
200
206
Python time: 0.0566 seconds
201
207
202
208
209
+
And here the output of an embedded `ARM beaglebone <http://beagleboard.org/Products/BeagleBone>`_ machine:
203
210
204
-
And here the output of an embedded `ARM beaglebone <http://beagleboard.org/Products/BeagleBone>`_ machine:
0 commit comments