Commit ad9f6367 authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2397 from gabrieldemarmiesse/test_early_binding_for_speed_2

Added tests to "Early binding for speed" part 2
parents f3f8b6fe 5472ad78
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
cdef int _area(self):
area = (self.x1 - self.x0) * (self.y1 - self.y0)
if area < 0:
area = -area
return area
def area(self):
return self._area()
def rectArea(x0, y0, x1, y1):
rect = Rectangle(x0, y0, x1, y1)
return rect.area()
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
cpdef int _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()
...@@ -30,26 +30,7 @@ In the :func:`rectArea` method, the call to :meth:`rect.area` and the ...@@ -30,26 +30,7 @@ In the :func:`rectArea` method, the call to :meth:`rect.area` and the
However, in Cython, it is possible to eliminate a lot of this overhead in cases However, in Cython, it is possible to eliminate a lot of this overhead in cases
where calls occur within Cython code. For example: where calls occur within Cython code. For example:
.. sourcecode:: cython .. literalinclude:: ../../examples/userguide/early_binding_for_speed/rectangle_cdef.pyx
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
cdef int _area(self):
cdef int area
area = (self.x1 - self.x0) * (self.y1 - self.y0)
if area < 0:
area = -area
return area
def area(self):
return self._area()
def rectArea(x0, y0, x1, y1):
cdef Rectangle rect
rect = Rectangle(x0, y0, x1, y1)
return rect._area()
Here, in the Rectangle extension class, we have defined two different area Here, in the Rectangle extension class, we have defined two different area
calculation methods, the efficient :meth:`_area` C method, and the calculation methods, the efficient :meth:`_area` C method, and the
...@@ -65,29 +46,12 @@ dual-access methods - methods that can be efficiently called at C level, but ...@@ -65,29 +46,12 @@ dual-access methods - methods that can be efficiently called at C level, but
can also be accessed from pure Python code at the cost of the Python access can also be accessed from pure Python code at the cost of the Python access
overheads. Consider this code: overheads. Consider this code:
.. sourcecode:: cython .. literalinclude:: ../../examples/userguide/early_binding_for_speed/rectangle_cpdef.pyx
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
cpdef int area(self):
cdef int area
area = (self.x1 - self.x0) * (self.y1 - self.y0)
if area < 0:
area = -area
return area
def rectArea(x0, y0, x1, y1):
cdef Rectangle rect
rect = Rectangle(x0, y0, x1, y1)
return rect.area()
.. note:: .. note::
in earlier versions of Cython, the :keyword:`cpdef` keyword is In earlier versions of Cython, the :keyword:`cpdef` keyword is
``rdef`` - but has the same effect). ``rdef`` - but has the same effect.
Here, we just have a single area method, declared as :keyword:`cpdef` to make it Here, we just have a single area method, declared as :keyword:`cpdef` to make it
efficiently callable as a C function, but still accessible from pure Python efficiently callable as a C function, but still accessible from pure Python
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment