Improvements of description. Thanks to sigmavirus24

This commit is contained in:
tommy3001
2014-02-23 23:32:13 +01:00
parent 33b428b8a5
commit b8f9923416
+23 -17
View File
@@ -69,12 +69,14 @@ Cython
------ ------
With `Cython <http://cython.org/>`_ you are able to write C and C++ modules for Python. It implements a superset of the Python language. With `Cython <http://cython.org/>`_ you are able to write C and C++ modules for Python. It implements a superset of the Python language.
With Cython you are also able to call C-functions and realize strong typing of variables and functions like float You are also able to call C-functions and realize declaration of variables and functions like in C. Here is an example:
(floating point numbers) or int (integer) definition of variables. Here is an example of strong typing with Cython:
.. code-block:: python .. code-block:: python
def primes(int kmax): def primes(int kmax):
"""Calculation of prime numbers with additional
Cython keywords"""
cdef int n, k, i cdef int n, k, i
cdef int p[1000] cdef int p[1000]
result = [] result = []
@@ -94,11 +96,14 @@ With Cython you are also able to call C-functions and realize strong typing of v
return result return result
This implementation of an algorithm to find prime numbers has some additional commands instead of the next one, which is implemented in pure Python: This implementation of an algorithm to find prime numbers has some additional keywords instead of the next one, which is implemented in pure Python:
.. code-block:: python .. code-block:: python
def primes( kmax):
def primes(kmax):
"""Calculation of prime numbers in standard Python syntax"""
p= range(1000) p= range(1000)
result = [] result = []
if kmax > 1000: if kmax > 1000:
@@ -120,28 +125,30 @@ This implementation of an algorithm to find prime numbers has some additional co
The only difference between the both algorithm is this part: The only difference between the both algorithm is this part:
Strong typing with Cython:
.. code-block:: python .. code-block:: python
#primes function with additional Cython code:
def primes(int kmax): def primes(int kmax):
"""Calculation of prime numbers with additional
Cython keywords"""
cdef int n, k, i cdef int n, k, i
cdef int p[1000] cdef int p[1000]
result = [] result = []
Normal variable definition in Python:
.. code-block:: python .. code-block:: python
#primes in standard Python syntax: def primes(kmax):
def primes( kmax): """Calculation of prime numbers in standard Python syntax"""
p= range(1000) p= range(1000)
result = [] result = []
What is the difference? In the upper Cython version you can see the definitions of the variable types like in standard C. What is the difference? In the upper Cython version you can see the declaration of the variable types and the integer array
For example `cdef int n,k,i` in line 3. This additional type definition (e.g. integer) allows the Cython compiler to generate 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)
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. allows the Cython compiler to generate more efficient C code from the second code. While standard Python code is saved in `*.py` files,
Cython code is saved in `*.pyx` files.
And what is with the speed? So lets try it! And what is with the speed? So lets try it!
@@ -169,7 +176,7 @@ And what is with the speed? So lets try it!
print "Python time: %s" %(t2-t1) print "Python time: %s" %(t2-t1)
Where is the magic? Here it is: These both lines need a remark:
.. code-block:: python .. code-block:: python
@@ -177,11 +184,10 @@ Where is the magic? Here it is:
pyximport.install() pyximport.install()
With the module `pyximport` you are able to import Cython `*.pyx` files, in this case `primesCy.pyx`, with the Cython The `pyximport` module allows you to import `pyx` files (e.g., `primesCy.pyx`) with the Cython-compiled version of the `primes` function.
version of the primes function.
The `pyximport.install()` command allows the Python interpreter to start the Cython compiler directly to generate C-code, The `pyximport.install()` command allows the Python interpreter to start the Cython compiler directly to generate C-code,
which is automatically compiled to a `*.so` C-library. ... and Cython is able to import this library for you in your Python-code. which is automatically compiled to a `*.so` C-library. Cython is able to import this library for you in your Python-code.
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. 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.
On a standard notebook (dualcore AMD E-450 1,6 GHz) the measured values are: On a standard notebook (dualcore AMD E-450 1,6 GHz) the measured values are: