Cython description and code example added #370

Merged
merged 6 commits into from Mar 5, 2014

4 participants

@tommy3001

I think that is possibly a next stone for our church.

@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 15, 2014
docs/scenarios/speed.rst
@@ -68,6 +68,124 @@ C Extensions
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 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):

Your indentation here is incorrect. The first line needs to be indented by 4 and the function body needs to be dedented by 4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 15, 2014
docs/scenarios/speed.rst
+(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

this too needs to be dedented by 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 15, 2014
docs/scenarios/speed.rst
+.. 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

Dedent the body of this if-statement too please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 15, 2014
docs/scenarios/speed.rst
+ 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

You have an extra space in front of the return statement or one too few in front of the n = n + 1 line. Fix the indentation please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tommy3001

You are right, i will do it. Thank you for the tip!

@tommy3001 tommy3001 closed this Feb 15, 2014
@sigmavirus24

@tommy3001 please do not open a new Pull Request. If you continue to make changes and push them to your fork, this Pull Request will update and we can continue the discussion in one place.

@sigmavirus24 sigmavirus24 reopened this Feb 15, 2014
@tommy3001

Fixed

@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 15, 2014
docs/scenarios/speed.rst
+ 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):

Same indentation fixes here as above please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 15, 2014
docs/scenarios/speed.rst
+ 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()

We like to encourage people not to use one-liners like this. Please remove the semi-colon and split into two lines and properly indent this code-block please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 15, 2014
docs/scenarios/speed.rst
+
+
+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 <http://beagleboard.org/Products/BeagleBone>`_ machine:

Can you explain the significance of an embedded ARM beaglebone machine? Why is this measurement significant? What is significant about it? Why not measure this on a different type of hardware?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on the diff Feb 15, 2014
docs/scenarios/speed.rst
+.. 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

Indentation

@tommy3001 seems you missed this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 15, 2014
docs/scenarios/speed.rst
+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):

Indentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 15, 2014
docs/scenarios/speed.rst
+ 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):

Indentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24

@tommy3001 you're almost there. Thanks for this! It looks like it will be an awesome contribution! 🍰

@tommy3001

Ok, right.

Thank you for your patience.

I will commit machine competition in future. :)

@sigmavirus24

You missed one last block of indentation necessary. I haven't read the copy closely, so there may be future changes necessary but if they're minor I might just merge this and handle them myself.

@tommy3001

Big sorry, I made some copy/paste misstakes from my code/ide source.

I added an additional benchmark. ;-)

@sigmavirus24

No need to apologize. Noone is upset. =)

@reicheltd

nice addition tommy

@sigmavirus24

You start most of your sentences with With Cython or really With .... I'm going to leave some feedback to help you reword them to avoid that pattern.

@sigmavirus24 sigmavirus24 commented on the diff Feb 22, 2014
docs/scenarios/speed.rst
@@ -68,6 +68,132 @@ C Extensions
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.
`Cython <http://cython.org/>`_ implements a superset of the Python language with which you are able to write C and C++ modules for Python. Cython also allows you to call functions from compiled C libraries. Using Cython allows you to take advantage of Python's strong typing of variables and operations. Here is an example of strong typing with Cython:

As a side note, I hope you realize that static typing is not strong typing. C is inherently weakly typed and Python strongly typed.

Missed this one too it seems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 22, 2014
docs/scenarios/speed.rst
@@ -68,6 +68,132 @@ C Extensions
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 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):

What does this function do? Could you include a doc-string with a description?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 22, 2014
docs/scenarios/speed.rst
+ 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):

Eliminate the space before kmax please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 22, 2014
docs/scenarios/speed.rst
+ 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:

Switch commands for keywords.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on the diff Feb 22, 2014
docs/scenarios/speed.rst
+ 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:

Notice that in the Cython version you declare integers and integer arrays for to be compiled into C types while also creating a Python list:

And one more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 22, 2014
docs/scenarios/speed.rst
+ 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:

Please remove this line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 22, 2014
docs/scenarios/speed.rst
+
+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:

Add indentation here please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 22, 2014
docs/scenarios/speed.rst
+ 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:

What does "magic" mean?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 22, 2014
docs/scenarios/speed.rst
+ 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
The `pyximport` module allows you to import `pyx` files (e.g., `primesCy.pyx`) with the Cython-compiled version of the `primes` function.

Then I would move the code block above this sentence below it and continue with your explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sigmavirus24 sigmavirus24 commented on an outdated diff Feb 22, 2014
docs/scenarios/speed.rst
+ 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.

The ellipses ... are unnecessary.

which is automatically compiled to a static object C file (`.so`). You are then able to import this library for you in Python easily and efficiently. Using the standard library's `time` function, you can compare the time between the two versions of this function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tommy3001

Done. Thank you for your support.

@kennethreitz
Owner

Thanks!

@kennethreitz kennethreitz merged commit 9427b02 into kennethreitz:master Mar 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment