Skip to content

Commit 26830d8

Browse files
committed
Add malloc example
1 parent 17a86be commit 26830d8

File tree

9 files changed

+445
-39
lines changed

9 files changed

+445
-39
lines changed

source-code/cython/Classes/points.pyx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
from math import sqrt
1+
from libc.math cimport sqrt
22

33
cdef class Point:
44

5-
cdef double x, y
5+
cdef double _x, _y
66

77
def __init__(self, x, y):
8-
self.x = x
9-
self.y = y
8+
self._x = x
9+
self._y = y
1010

11-
def distance(self):
12-
return sqrt(self.x**2 + self.y**2)
11+
cpdef distance(self, other):
12+
return sqrt((self._x - other._x)**2 + (self._y - other._y)**2)
1313

1414
property x:
1515
def __get__(self):
16-
return self.x
16+
return self._x
1717
def __set__(self, value):
18-
self.x = float(value)
18+
self._x = float(value)
1919

2020
property y:
2121
def __get__(self):
22-
return self.y
22+
return self._y
2323
def __set__(self, value):
24-
self.y = float(value)
24+
self._y = float(value)
2525

2626

2727
class ColoredPoint(Point):

source-code/cython/Classes/points_pure.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,56 @@
11
import cython
2-
from math import sqrt
2+
from cython.cimports.libc.math import sqrt
3+
4+
5+
@cython.cfunc
6+
def _min_max_distance(points: cython.list) -> cython.tuple[cython.double, cython.double]:
7+
min_distance : cython.double = 2.0
8+
max_distance : cython.double = 0.0
9+
for i in range(len(points)):
10+
p1 : Point = points[i]
11+
for j in range(i+1, len(points)):
12+
p2 : Point = points[j]
13+
distance = sqrt((p1._x - p2._x)*(p1._x - p2._x) + (p1._y - p2._y)*(p1._y - p2._y))
14+
if distance < min_distance:
15+
min_distance = distance
16+
if distance > max_distance:
17+
max_distance = distance
18+
return min_distance, max_distance
19+
320

421
@cython.cclass
522
class Point:
623

7-
x: cython.float
8-
y: cython.float
24+
_x: cython.double
25+
_y: cython.double
926

10-
def __init__(self, x: cython.float, y: cython.float) -> None:
11-
self.x = x
12-
self.y = y
27+
def __init__(self, x: cython.double, y: cython.double) -> None:
28+
self._x = x
29+
self._y = y
1330

14-
def distance(self) -> cython.float:
15-
return sqrt(self.x**2 + self.y**2)
31+
@cython.ccall
32+
def distance(self, other: Point) -> cython.double:
33+
return sqrt((self._x - other._x)*(self._x - other._x) + (self._y - other._y)*(self._y - other._y))
1634

35+
@staticmethod
36+
def min_max_distance(points: list) -> tuple[float, float]:
37+
return _min_max_distance(points)
38+
1739
@property
18-
def x(self) -> cython.float:
19-
return self.x
40+
def x(self) -> cython.double:
41+
return self._x
2042

2143
@x.setter
22-
def x(self, value: cython.float) -> None:
23-
self.x = float(value)
44+
def x(self, value: cython.double) -> None:
45+
self._x = float(value)
2446

2547
@property
26-
def y(self) -> cython.float:
27-
return self.y
48+
def y(self) -> cython.double:
49+
return self._y
2850

2951
@y.setter
30-
def y(self, value: cython.float) -> None:
31-
self.y = float(value)
52+
def y(self, value: cython.double) -> None:
53+
self._y = float(value)
3254

3355
def __str__(self) -> str:
3456
return f"Point({self.x}, {self.y})"

source-code/cython/Primes/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
primes_cython.c
22
primes_cython3.c
33
primes_pure_python.c
4+
primes_malloc.c

source-code/cython/Primes/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
VERSION = cpython-311-x86_64-linux-gnu
22
PRIMES_LIBS = primes_cython.$(VERSION).so primes_cython3.$(VERSION).so \
3-
primes_pure_python.$(VERSION).so
3+
primes_pure_python.$(VERSION).so primes_pure_malloc.$(VERSION).so \
4+
primes_malloc.$(VERSION).so
45

56
all: $(PRIMES_LIBS)
67

@@ -9,5 +10,5 @@ $(PRIMES_LIBS): primes_cython.pyx primes_pure_python.py
910

1011
clean:
1112
python setup.py clean
12-
$(RM) primes_cython.c primes_cython3.c primes_pure_python.c $(PRIMES_LIBS)
13+
$(RM) primes_cython.c primes_cython3.c primes_pure_python.c primes_malloc.c $(PRIMES_LIBS)
1314
$(RM) -r build/

source-code/cython/Primes/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Motivating example for using Cython
1010
1. `primes_pure_python.py`: Cython pure Python implementation of the
1111
primes function.
1212
1. `setup.py`: Python build script.
13+
1. `primes_malloc.pyx`: illustration of using `malloc` in Cython.
14+
1. `primes_pure_malloc.pyx`: illustration of using `malloc` in Cython's
15+
pure Python syntax.
1316
1. `Makefile`: make file to build the extension.
1417
1. `time_all.sh`: timings with hyperfine, **Note:** due to short runtimes
1518
these timings are donated by Python interpreter startup times and

0 commit comments

Comments
 (0)