Skip to content

Commit

Permalink
Merge pull request #2365 from gabrieldemarmiesse/test_early_binding_f…
Browse files Browse the repository at this point in the history
…or_speed_1

Added tests to "Early binding for speed" part 1
  • Loading branch information
scoder committed Jun 17, 2018
2 parents 771fe73 + 0cbd5d4 commit 5c04c1a
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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 5c04c1a

Please sign in to comment.