Improve speed in some modules by oscargus · Pull Request #22678 · matplotlib/matplotlib · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/matplotlib/cbook/__init__.py
6 changes: 3 additions & 3 deletions lib/matplotlib/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ def _set_pentagon(self):
self._path = polypath
else:
verts = polypath.vertices
y = (1 + np.sqrt(5)) / 4.
y = (1 + 5 ** (1 / 2)) / 4.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced of all these sqrt micro-optimizations. IMHO it makes the code less readable, and the performance gain is minimal. For example here

The sqrt is less than 2% of the the time of the immediate surrounding code in the function:

In [21]: %%timeit
    ...: Affine2D().scale(0.5)
    ...: polypath = Path.unit_regular_polygon(5)
    ...: verts = polypath.vertices
    ...: top = Path(verts[[0, 1, 4, 0]])
    ...: bottom = Path(verts[[1, 2, 3, 4, 1]])
    ...: left = Path([verts[0], verts[1], verts[2], [0, -y], verts[0]])
    ...: right = Path([verts[0], verts[4], verts[3], [0, -y], verts[0]])
    ...: 
61.1 µs ± 228 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [22]: %timeit np.sqrt(5)
2.36 µs ± 35 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

and that function _set_pentagon() again is only used to create a new marker object, which in itself is quite rare. So I bet the performance gain is not measurable in any real example.

top = Path(verts[[0, 1, 4, 0]])
bottom = Path(verts[[1, 2, 3, 4, 1]])
left = Path([verts[0], verts[1], verts[2], [0, -y], verts[0]])
Expand Down Expand Up @@ -747,7 +747,7 @@ def _set_hexagon2(self):
else:
verts = polypath.vertices
# not drawing inside lines
x, y = np.sqrt(3) / 4, 3 / 4.
x, y = 3 ** (1 / 2) / 4, 3 / 4.
top = Path(verts[[1, 0, 5, 4, 1]])
bottom = Path(verts[1:5])
left = Path(np.concatenate([
Expand All @@ -772,7 +772,7 @@ def _set_octagon(self):
self._transform.rotate_deg(22.5)
self._path = polypath
else:
x = np.sqrt(2.) / 4.
x = 2 ** (1 / 2) / 4.
self._path = self._alt_path = Path(
[[0, -1], [0, 1], [-x, 1], [-1, x],
[-1, -x], [-x, -1], [0, -1]])
Expand Down
94 changes: 49 additions & 45 deletions lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,45 +822,48 @@ def circle(cls, center=(0., 0.), radius=1., readonly=False):
Bezier Cubic Splines <https://www.tinaja.com/glib/ellipse4.pdf>`_.
"""
MAGIC = 0.2652031
SQRTHALF = np.sqrt(0.5)
SQRTHALF = (1 / 2) ** (1 / 2)
MAGIC45 = SQRTHALF * MAGIC
MAGICSUM = SQRTHALF + MAGIC45
MAGICDIFF = SQRTHALF - MAGIC45

vertices = np.array([[0.0, -1.0],

@anntzer anntzer Apr 24, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I would just write this is as vertices = np.array([...]) without even bothering with the dtype=float which is unneeded; this also avoids a temporary variable (mostly avoids having to keep track of it mentally). Ditto below.

vertices = ((0.0, -1.0),

[MAGIC, -1.0],
[SQRTHALF-MAGIC45, -SQRTHALF-MAGIC45],
[SQRTHALF, -SQRTHALF],
(MAGIC, -1.0),
(MAGICDIFF, -MAGICSUM),
(SQRTHALF, -SQRTHALF),

[SQRTHALF+MAGIC45, -SQRTHALF+MAGIC45],
[1.0, -MAGIC],
[1.0, 0.0],
(MAGICSUM, -MAGICDIFF),
(1.0, -MAGIC),
(1.0, 0.0),

[1.0, MAGIC],
[SQRTHALF+MAGIC45, SQRTHALF-MAGIC45],
[SQRTHALF, SQRTHALF],
(1.0, MAGIC),
(MAGICSUM, MAGICDIFF),
(SQRTHALF, SQRTHALF),

[SQRTHALF-MAGIC45, SQRTHALF+MAGIC45],
[MAGIC, 1.0],
[0.0, 1.0],
(MAGICDIFF, MAGICSUM),
(MAGIC, 1.0),
(0.0, 1.0),

[-MAGIC, 1.0],
[-SQRTHALF+MAGIC45, SQRTHALF+MAGIC45],
[-SQRTHALF, SQRTHALF],
(-MAGIC, 1.0),
(-MAGICDIFF, MAGICSUM),
(-SQRTHALF, SQRTHALF),

[-SQRTHALF-MAGIC45, SQRTHALF-MAGIC45],
[-1.0, MAGIC],
[-1.0, 0.0],
(-MAGICSUM, MAGICDIFF),
(-1.0, MAGIC),
(-1.0, 0.0),

[-1.0, -MAGIC],
[-SQRTHALF-MAGIC45, -SQRTHALF+MAGIC45],
[-SQRTHALF, -SQRTHALF],
(-1.0, -MAGIC),
(-MAGICSUM, -MAGICDIFF),
(-SQRTHALF, -SQRTHALF),

[-SQRTHALF+MAGIC45, -SQRTHALF-MAGIC45],
[-MAGIC, -1.0],
[0.0, -1.0],
(-MAGICDIFF, -MAGICSUM),
(-MAGIC, -1.0),
(0.0, -1.0),

[0.0, -1.0]],
dtype=float)
(0.0, -1.0))

vertices = np.array(vertices, dtype=float)

codes = [cls.CURVE4] * 26
codes[0] = cls.MOVETO
Expand All @@ -878,31 +881,32 @@ def unit_circle_righthalf(cls):
"""
if cls._unit_circle_righthalf is None:
MAGIC = 0.2652031
SQRTHALF = np.sqrt(0.5)
SQRTHALF = (1 / 2) ** (1 / 2)
MAGIC45 = SQRTHALF * MAGIC
MAGICSUM = SQRTHALF + MAGIC45
MAGICDIFF = SQRTHALF - MAGIC45

vertices = np.array(
[[0.0, -1.0],
vertices = ((0.0, -1.0),

[MAGIC, -1.0],
[SQRTHALF-MAGIC45, -SQRTHALF-MAGIC45],
[SQRTHALF, -SQRTHALF],
(MAGIC, -1.0),
(MAGICDIFF, -MAGICSUM),
(SQRTHALF, -SQRTHALF),

[SQRTHALF+MAGIC45, -SQRTHALF+MAGIC45],
[1.0, -MAGIC],
[1.0, 0.0],
(MAGICSUM, -MAGICDIFF),
(1.0, -MAGIC),
(1.0, 0.0),

[1.0, MAGIC],
[SQRTHALF+MAGIC45, SQRTHALF-MAGIC45],
[SQRTHALF, SQRTHALF],
(1.0, MAGIC),
(MAGICSUM, MAGICDIFF),
(SQRTHALF, SQRTHALF),

[SQRTHALF-MAGIC45, SQRTHALF+MAGIC45],
[MAGIC, 1.0],
[0.0, 1.0],
(MAGICDIFF, MAGICSUM),
(MAGIC, 1.0),
(0.0, 1.0),

[0.0, -1.0]],
(0.0, -1.0))

float)
vertices = np.array(vertices, dtype=float)

codes = np.full(14, cls.CURVE4, dtype=cls.code_type)
codes[0] = cls.MOVETO
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/patheffects.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ def __init__(self, offset=(0, 0),
to your right, and 180 behind you.
length : float, default: 1.414
The length of the tick relative to spacing.
Recommended length = 1.414 (sqrt(2)) when angle=45, length=1.0
when angle=90 and length=2.0 when angle=60.
Recommended length = 1.414 (:math:`\\sqrt{2}`) when angle=45,
length=1.0 when angle=90 and length=2.0 when angle=60.
**kwargs
Extra keywords are stored and passed through to
:meth:`AbstractPathEffect._update_gc`.
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/projections/polar.py