From 29a3643ab485171cf4d3ede567628a4dbc91e9fd Mon Sep 17 00:00:00 2001 From: tommy3001 Date: Sat, 15 Feb 2014 13:08:42 +0100 Subject: [PATCH 1/6] Cython example added for strong typing --- docs/scenarios/speed.rst | 118 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/docs/scenarios/speed.rst b/docs/scenarios/speed.rst index 6aaa96b..af8f355 100644 --- a/docs/scenarios/speed.rst +++ b/docs/scenarios/speed.rst @@ -68,6 +68,124 @@ C Extensions Cython ------ +With `Cython `_ 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 +(floating point numbers) or int (integer) definition of variables. Here is an example of strong typing with Cython: + +.. code-block:: python + + def primes(int kmax): + cdef int n, k, i + cdef int p[1000] + result = [] + if kmax > 1000: + kmax = 1000 + k = 0 + n = 2 + while k < kmax: + i = 0 + while i < k and n % p[i] != 0: + i = i + 1 + if i == k: + p[k] = n + k = k + 1 + result.append(n) + n = n + 1 + 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: + +.. code-block:: python + + def primes( kmax): + p= range(1000) + result = [] + if kmax > 1000: + kmax = 1000 + k = 0 + n = 2 + while k < kmax: + i = 0 + while i < k and n % p[i] != 0: + i = i + 1 + if i == k: + p[k] = n + k = k + 1 + result.append(n) + n = n + 1 + return result + + +The only difference between the both algorithm is this part: + +Strong typing with Cython: + +.. code-block:: python + + #primes function with additional Cython code: + def primes(int kmax): + cdef int n, k, i + cdef int p[1000] + result = [] + + +Normal variable definition in Python: + +.. code-block:: python + + #primes in standard Python syntax: + def primes( kmax): + p= range(1000) + result = [] + + +What is the difference? In the upper Cython version you can see the definitions of the variable types like in standard C. +For example `cdef int n,k,i` in line 3. This additional type definition (e.g. integer) allows the Cython compiler to generate +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. + +And what is with the speed? So lets try it! + +.. code-block:: python + + import time + #activate pyx compiler + import pyximport; pyximport.install() + #primes implemented with Cython + import primesCy + #primes implemented with Python + import primes + + print "Cython:" + t1= time.time() + print primesCy.primes(500) + t2= time.time() + print "Cython time: %s" %(t2-t1) + print "" + print "Python" + t1= time.time() + print primes.primes(500) + t2= time.time() + print "Python time: %s" %(t2-t1) + + +Where is the magic? Here it is: + +.. code-block:: python + + import pyximport; pyximport.install() + + +With the module `pyximport` you are able to import Cython `*.pyx` files, in this case `primesCy.pyx`, with the Cython +version of the primes function. +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. +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. + +Here is the output of an embedded `ARM beaglebone `_ machine: + +Cython time: 0.0196 seconds + +Python time: 0.3302 seconds Pyrex ----- From a30b491b5624a7be6f49352659ae18bc48c1c106 Mon Sep 17 00:00:00 2001 From: tommy3001 Date: Sat, 15 Feb 2014 17:58:40 +0100 Subject: [PATCH 2/6] indentation fixed --- docs/scenarios/speed.rst | 84 ++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/docs/scenarios/speed.rst b/docs/scenarios/speed.rst index af8f355..5056086 100644 --- a/docs/scenarios/speed.rst +++ b/docs/scenarios/speed.rst @@ -74,46 +74,48 @@ With Cython you are also able to call C-functions and realize strong typing of v .. code-block:: python - def primes(int kmax): - cdef int n, k, i - cdef int p[1000] - result = [] - if kmax > 1000: - kmax = 1000 - k = 0 - n = 2 - while k < kmax: - i = 0 - while i < k and n % p[i] != 0: - i = i + 1 - if i == k: - p[k] = n - k = k + 1 - result.append(n) - n = n + 1 - return result + def primes(int kmax): + cdef int n, k, i + cdef int p[1000] + result = [] + if kmax > 1000: + kmax = 1000 + k = 0 + n = 2 + while k < kmax: + i = 0 + while i < k and n % p[i] != 0: + i = i + 1 + if i == k: + p[k] = n + k = k + 1 + result.append(n) + n = n + 1 + 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: .. code-block:: python - def primes( kmax): - p= range(1000) - result = [] - if kmax > 1000: - kmax = 1000 - k = 0 - n = 2 - while k < kmax: - i = 0 - while i < k and n % p[i] != 0: - i = i + 1 - if i == k: - p[k] = n - k = k + 1 - result.append(n) - n = n + 1 - return result + def primes( kmax): + p= range(1000) + result = [] + if kmax > 1000: + kmax = 1000 + k = 0 + n = 2 + while k < kmax: + i = 0 + while i < k and n % p[i] != 0: + i = i + 1 + if i == k: + p[k] = n + k = k + 1 + result.append(n) + n = n + 1 + return result + The only difference between the both algorithm is this part: @@ -124,10 +126,9 @@ Strong typing with Cython: #primes function with additional Cython code: def primes(int kmax): - cdef int n, k, i - cdef int p[1000] - result = [] - + cdef int n, k, i + cdef int p[1000] + result = [] Normal variable definition in Python: @@ -135,9 +136,8 @@ Normal variable definition in Python: #primes in standard Python syntax: def primes( kmax): - p= range(1000) - result = [] - + p= range(1000) + result = [] What is the difference? In the upper Cython version you can see the definitions of the variable types like in standard C. For example `cdef int n,k,i` in line 3. This additional type definition (e.g. integer) allows the Cython compiler to generate From 7ef55e755f8c8e714ade2f1ebbf21ebab585e7dd Mon Sep 17 00:00:00 2001 From: tommy3001 Date: Sat, 15 Feb 2014 18:38:43 +0100 Subject: [PATCH 3/6] minor changes (indentation fixed) --- docs/scenarios/speed.rst | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/scenarios/speed.rst b/docs/scenarios/speed.rst index 5056086..f8a37c9 100644 --- a/docs/scenarios/speed.rst +++ b/docs/scenarios/speed.rst @@ -124,8 +124,8 @@ Strong typing with Cython: .. code-block:: python - #primes function with additional Cython code: - def primes(int kmax): + #primes function with additional Cython code: + def primes(int kmax): cdef int n, k, i cdef int p[1000] result = [] @@ -135,7 +135,7 @@ Normal variable definition in Python: .. code-block:: python #primes in standard Python syntax: - def primes( kmax): + def primes( kmax): p= range(1000) result = [] @@ -149,7 +149,8 @@ And what is with the speed? So lets try it! import time #activate pyx compiler - import pyximport; pyximport.install() + import pyximport + pyximport.install() #primes implemented with Cython import primesCy #primes implemented with Python @@ -172,14 +173,15 @@ Where is the magic? Here it is: .. code-block:: python - import pyximport; pyximport.install() + import pyximport + pyximport.install() With the module `pyximport` you are able to import Cython `*.pyx` files, in this case `primesCy.pyx`, with the Cython version of the primes function. 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. -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. +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. Here is the output of an embedded `ARM beaglebone `_ machine: From d30830009a5df0c10019ced46f036478dcdc5807 Mon Sep 17 00:00:00 2001 From: tommy3001 Date: Sat, 15 Feb 2014 19:33:00 +0100 Subject: [PATCH 4/6] Cython: additional benchmark for standard CPU --- docs/scenarios/speed.rst | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/scenarios/speed.rst b/docs/scenarios/speed.rst index f8a37c9..7a2ef88 100644 --- a/docs/scenarios/speed.rst +++ b/docs/scenarios/speed.rst @@ -183,7 +183,13 @@ The `pyximport.install()` command allows the Python interpreter to start the Cyt which is automatically compiled to a `*.so` C-library. ... and 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 this 2 different calls to find 500 prime numbers. -Here is the output of an embedded `ARM beaglebone `_ machine: +On a standard notebook (dualcore AMD E-450 1,6 GHz) the measured values are: + +Cython time: 0.0054 seconds + +Python time: 0.0566 seconds + +And here the output of an embedded `ARM beaglebone `_ machine: Cython time: 0.0196 seconds From 33b428b8a5c352f9c6d902c352f3d477bb1f7bf2 Mon Sep 17 00:00:00 2001 From: tommy3001 Date: Sun, 16 Feb 2014 17:50:07 +0100 Subject: [PATCH 5/6] misspelling in description --- docs/scenarios/speed.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/scenarios/speed.rst b/docs/scenarios/speed.rst index 7a2ef88..308d27c 100644 --- a/docs/scenarios/speed.rst +++ b/docs/scenarios/speed.rst @@ -181,7 +181,7 @@ With the module `pyximport` you are able to import Cython `*.pyx` files, in this version of the primes function. 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. -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. +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. On a standard notebook (dualcore AMD E-450 1,6 GHz) the measured values are: From b8f99234165cd520cdda5f2e440b6066492fa772 Mon Sep 17 00:00:00 2001 From: tommy3001 Date: Sun, 23 Feb 2014 23:32:13 +0100 Subject: [PATCH 6/6] Improvements of description. Thanks to sigmavirus24 --- docs/scenarios/speed.rst | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/docs/scenarios/speed.rst b/docs/scenarios/speed.rst index 308d27c..05130c8 100644 --- a/docs/scenarios/speed.rst +++ b/docs/scenarios/speed.rst @@ -69,12 +69,14 @@ Cython ------ With `Cython `_ 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 -(floating point numbers) or int (integer) definition of variables. Here is an example of strong typing with Cython: +You are also able to call C-functions and realize declaration of variables and functions like in C. Here is an example: .. code-block:: python def primes(int kmax): + """Calculation of prime numbers with additional + Cython keywords""" + cdef int n, k, i cdef int p[1000] result = [] @@ -94,11 +96,14 @@ With Cython you are also able to call C-functions and realize strong typing of v 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 - def primes( kmax): + + def primes(kmax): + """Calculation of prime numbers in standard Python syntax""" + p= range(1000) result = [] 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: -Strong typing with Cython: .. code-block:: python - #primes function with additional Cython code: def primes(int kmax): + """Calculation of prime numbers with additional + Cython keywords""" + cdef int n, k, i cdef int p[1000] result = [] -Normal variable definition in 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) result = [] -What is the difference? In the upper Cython version you can see the definitions of the variable types like in standard C. -For example `cdef int n,k,i` in line 3. This additional type definition (e.g. integer) allows the Cython compiler to generate -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. +What is the difference? In the upper Cython version you can see the declaration of the variable types and the integer array +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) +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! @@ -169,7 +176,7 @@ And what is with the speed? So lets try it! print "Python time: %s" %(t2-t1) -Where is the magic? Here it is: +These both lines need a remark: .. code-block:: python @@ -177,11 +184,10 @@ Where is the magic? Here it is: pyximport.install() -With the module `pyximport` you are able to import Cython `*.pyx` files, in this case `primesCy.pyx`, with the Cython -version of the primes function. +The `pyximport` module allows you to import `pyx` files (e.g., `primesCy.pyx`) with the Cython-compiled version of the `primes` function. 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. -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. +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 this 2 different calls to find 500 prime numbers. On a standard notebook (dualcore AMD E-450 1,6 GHz) the measured values are: