Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
ad9f6367
Commit
ad9f6367
authored
Jun 22, 2018
by
scoder
Committed by
GitHub
Jun 22, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2397 from gabrieldemarmiesse/test_early_binding_for_speed_2
Added tests to "Early binding for speed" part 2
parents
f3f8b6fe
5472ad78
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
40 deletions
+45
-40
docs/examples/userguide/early_binding_for_speed/rectangle_cdef.pyx
...ples/userguide/early_binding_for_speed/rectangle_cdef.pyx
+22
-0
docs/examples/userguide/early_binding_for_speed/rectangle_cpdef.pyx
...les/userguide/early_binding_for_speed/rectangle_cpdef.pyx
+19
-0
docs/src/userguide/early_binding_for_speed.rst
docs/src/userguide/early_binding_for_speed.rst
+4
-40
No files found.
docs/examples/userguide/early_binding_for_speed/rectangle_cdef.pyx
0 → 100644
View file @
ad9f6367
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
()
docs/examples/userguide/early_binding_for_speed/rectangle_cpdef.pyx
0 → 100644
View file @
ad9f6367
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
()
docs/src/userguide/early_binding_for_speed.rst
View file @
ad9f6367
...
@@ -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::
i
n earlier versions of Cython, the :keyword:`cpdef` keyword is
I
n 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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment