Skip to content

Commit

Permalink
Moved the first example of "early binding for speed" in the examples …
Browse files Browse the repository at this point in the history
…directory for testing.
  • Loading branch information
gabrieldemarmiesse committed Jun 17, 2018
1 parent 46b3955 commit 0cbd5d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
19 changes: 19 additions & 0 deletions docs/examples/userguide/early_binding_for_speed/rectangle.pyx
@@ -0,0 +1,19 @@
cdef class Rectangle:
cdef int x0, y0
cdef int x1, y1

def __init__(self, int x0, int y0, int x1, int y1):
self.x0 = x0
self.y0 = y0
self.x1 = x1
self.y1 = y1

def area(self):
area = (self.x1 - self.x0) * (self.y1 - self.y0)
if area < 0:
area = -area
return area

def rectArea(x0, y0, x1, y1):
rect = Rectangle(x0, y0, x1, y1)
return rect.area()
17 changes: 1 addition & 16 deletions docs/src/userguide/early_binding_for_speed.rst
Expand Up @@ -22,22 +22,7 @@ use of 'early binding' programming techniques.

For example, consider the following (silly) code example:

.. sourcecode:: cython

cdef class Rectangle:
cdef int x0, y0
cdef int x1, y1
def __init__(self, int x0, int y0, int x1, int y1):
self.x0 = x0; self.y0 = y0; self.x1 = x1; self.y1 = y1
def area(self):
area = (self.x1 - self.x0) * (self.y1 - self.y0)
if area < 0:
area = -area
return area

def rectArea(x0, y0, x1, y1):
rect = Rectangle(x0, y0, x1, y1)
return rect.area()
.. literalinclude:: ../../examples/userguide/early_binding_for_speed/rectangle.pyx

In the :func:`rectArea` method, the call to :meth:`rect.area` and the
:meth:`.area` method contain a lot of Python overhead.
Expand Down

0 comments on commit 0cbd5d4

Please sign in to comment.