Skip to content

Commit b8f9923

Browse files
author
tommy3001
committed
Improvements of description. Thanks to sigmavirus24
1 parent 33b428b commit b8f9923

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

docs/scenarios/speed.rst

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,14 @@ Cython
6969
------
7070

7171
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:
7473

7574
.. code-block:: python
7675
7776
def primes(int kmax):
77+
"""Calculation of prime numbers with additional
78+
Cython keywords"""
79+
7880
cdef int n, k, i
7981
cdef int p[1000]
8082
result = []
@@ -94,11 +96,14 @@ With Cython you are also able to call C-functions and realize strong typing of v
9496
return result
9597
9698
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:
98100

99101
.. code-block:: python
100102
101-
def primes( kmax):
103+
104+
def primes(kmax):
105+
"""Calculation of prime numbers in standard Python syntax"""
106+
102107
p= range(1000)
103108
result = []
104109
if kmax > 1000:
@@ -120,28 +125,30 @@ This implementation of an algorithm to find prime numbers has some additional co
120125
121126
The only difference between the both algorithm is this part:
122127

123-
Strong typing with Cython:
124128

125129
.. code-block:: python
126130
127-
#primes function with additional Cython code:
128131
def primes(int kmax):
132+
"""Calculation of prime numbers with additional
133+
Cython keywords"""
134+
129135
cdef int n, k, i
130136
cdef int p[1000]
131137
result = []
132138
133-
Normal variable definition in Python:
134139
135140
.. code-block:: python
136141
137-
#primes in standard Python syntax:
138-
def primes( kmax):
142+
def primes(kmax):
143+
"""Calculation of prime numbers in standard Python syntax"""
144+
139145
p= range(1000)
140146
result = []
141147
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.
145152

146153
And what is with the speed? So lets try it!
147154

@@ -169,19 +176,18 @@ And what is with the speed? So lets try it!
169176
print "Python time: %s" %(t2-t1)
170177
171178
172-
Where is the magic? Here it is:
179+
These both lines need a remark:
173180

174181
.. code-block:: python
175182
176183
import pyximport
177184
pyximport.install()
178185
179186
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.
182188
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.
185191

186192
On a standard notebook (dualcore AMD E-450 1,6 GHz) the measured values are:
187193

0 commit comments

Comments
 (0)