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
+23-17Lines changed: 23 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -69,12 +69,14 @@ Cython
69
69
------
70
70
71
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:
72
+
You are also able to call C-functions and realize declaration of variables and functions like in C. Here is an example:
74
73
75
74
.. code-block:: python
76
75
77
76
defprimes(int kmax):
77
+
"""Calculation of prime numbers with additional
78
+
Cython keywords"""
79
+
78
80
cdef int n, k, i
79
81
cdef int p[1000]
80
82
result = []
@@ -94,11 +96,14 @@ With Cython you are also able to call C-functions and realize strong typing of v
94
96
return result
95
97
96
98
97
-
This implementation of an algorithm to find prime numbers has some additional commands instead of the next one, which is implemented in pure Python:
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:
98
100
99
101
.. code-block:: python
100
102
101
-
defprimes( kmax):
103
+
104
+
defprimes(kmax):
105
+
"""Calculation of prime numbers in standard Python syntax"""
106
+
102
107
p=range(1000)
103
108
result = []
104
109
if kmax >1000:
@@ -120,28 +125,30 @@ This implementation of an algorithm to find prime numbers has some additional co
120
125
121
126
The only difference between the both algorithm is this part:
122
127
123
-
Strong typing with Cython:
124
128
125
129
.. code-block:: python
126
130
127
-
#primes function with additional Cython code:
128
131
defprimes(int kmax):
132
+
"""Calculation of prime numbers with additional
133
+
Cython keywords"""
134
+
129
135
cdef int n, k, i
130
136
cdef int p[1000]
131
137
result = []
132
138
133
-
Normal variable definition in Python:
134
139
135
140
.. code-block:: python
136
141
137
-
#primes in standard Python syntax:
138
-
defprimes( kmax):
142
+
defprimes(kmax):
143
+
"""Calculation of prime numbers in standard Python syntax"""
144
+
139
145
p=range(1000)
140
146
result = []
141
147
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.
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.
145
152
146
153
And what is with the speed? So lets try it!
147
154
@@ -169,19 +176,18 @@ And what is with the speed? So lets try it!
169
176
print"Python time: %s"%(t2-t1)
170
177
171
178
172
-
Where is the magic? Here it is:
179
+
These both lines need a remark:
173
180
174
181
.. code-block:: python
175
182
176
183
import pyximport
177
184
pyximport.install()
178
185
179
186
180
-
With the module `pyximport` you are able to import Cython `*.pyx` files, in this case `primesCy.pyx`, with the Cython
181
-
version of the primes function.
187
+
The `pyximport` module allows you to import `pyx` files (e.g., `primesCy.pyx`) with the Cython-compiled version of the `primes` function.
182
188
The `pyximport.install()` command allows the Python interpreter to start the Cython compiler directly to generate C-code,
183
-
which is automatically compiled to a `*.so` C-library. ... and Cython is able to import this library for you in your Python-code.
184
-
Very easy and very efficient. With the `time.time()` function you are able to compare the time between these 2 different calls to find 500 prime numbers.
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.
185
191
186
192
On a standard notebook (dualcore AMD E-450 1,6 GHz) the measured values are:
0 commit comments