Skip to content

Commit 79a6dd7

Browse files
authored
Merge pull request #66 from petercorke/perf/vertex-coord-array
perf: a vertex makes its coordinate with np.array, not np.r_
2 parents d41fd62 + 0a9b654 commit 79a6dd7

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎src/pgraph/PGraph.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4518,7 +4518,8 @@ def __init__(self, coord: ArrayLike | None = None, name: str | None = None):
45184518
# invalidate; writing the attributes directly avoids the cost of
45194519
# ``__setattr__`` on each of them.
45204520
self.__dict__.update(
4521-
coord=None if coord is None else np.r_[coord],
4521+
# np.array is a tenth of the cost of np.r_ for the same result here
4522+
coord=None if coord is None else np.array(coord, ndmin=1),
45224523
_name=name,
45234524
_label=None,
45244525
_connectivitychange=True,

‎tests/test_vertex_init.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,37 @@ def test_the_coordinate_is_a_float_array_copy(self):
3939
self.assertEqual(v.coord[0], 1.0) # not the caller's array
4040
self.assertIsInstance(v.coord, np.ndarray)
4141

42+
def test_the_coordinate_is_what_np_r_made(self):
43+
# the coordinate used to be made with np.r_[coord]; for everything a
44+
# coordinate can be it is the same array, in shape, type and values
45+
for coord in (
46+
[1, 2],
47+
[1.5, 2],
48+
(1, 2, 3),
49+
5,
50+
2.5,
51+
np.array([1.0, 2.0]),
52+
np.array([1, 2], dtype=np.int32),
53+
range(3),
54+
[[1, 2], [3, 4]],
55+
):
56+
with self.subTest(coord=repr(coord)):
57+
v = Vertex(coord=coord)
58+
expected = np.r_[coord]
59+
self.assertEqual(v.coord.shape, expected.shape)
60+
self.assertEqual(v.coord.dtype, expected.dtype)
61+
np.testing.assert_array_equal(v.coord, expected)
62+
63+
def test_a_scalar_is_a_vector_of_one(self):
64+
self.assertEqual(Vertex(coord=7).coord.shape, (1,))
65+
66+
def test_an_array_given_is_copied(self):
67+
c = np.array([1, 2])
68+
v = Vertex(coord=c)
69+
c[0] = 99
70+
self.assertEqual(v.coord[0], 1)
71+
self.assertFalse(np.shares_memory(v.coord, c))
72+
4273
def test_the_lists_are_not_shared_between_vertices(self):
4374
a, b = Vertex(), Vertex()
4475
self.assertIsNot(a._edgelist, b._edgelist)

0 commit comments

Comments
 (0)