mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 23:00:18 +00:00
minor changes (indentation fixed)
This commit is contained in:
@@ -124,8 +124,8 @@ Strong typing with Cython:
|
|||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
#primes function with additional Cython code:
|
#primes function with additional Cython code:
|
||||||
def primes(int kmax):
|
def primes(int kmax):
|
||||||
cdef int n, k, i
|
cdef int n, k, i
|
||||||
cdef int p[1000]
|
cdef int p[1000]
|
||||||
result = []
|
result = []
|
||||||
@@ -135,7 +135,7 @@ Normal variable definition in Python:
|
|||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
#primes in standard Python syntax:
|
#primes in standard Python syntax:
|
||||||
def primes( kmax):
|
def primes( kmax):
|
||||||
p= range(1000)
|
p= range(1000)
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
@@ -149,7 +149,8 @@ And what is with the speed? So lets try it!
|
|||||||
|
|
||||||
import time
|
import time
|
||||||
#activate pyx compiler
|
#activate pyx compiler
|
||||||
import pyximport; pyximport.install()
|
import pyximport
|
||||||
|
pyximport.install()
|
||||||
#primes implemented with Cython
|
#primes implemented with Cython
|
||||||
import primesCy
|
import primesCy
|
||||||
#primes implemented with Python
|
#primes implemented with Python
|
||||||
@@ -172,14 +173,15 @@ Where is the magic? Here it is:
|
|||||||
|
|
||||||
.. code-block:: python
|
.. 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
|
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.
|
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. ... 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 <http://beagleboard.org/Products/BeagleBone>`_ machine:
|
Here is the output of an embedded `ARM beaglebone <http://beagleboard.org/Products/BeagleBone>`_ machine:
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user