Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6afd28c

Browse filesBrowse files
committed
Don't call np.identity() in transforms.
Speeds up setting up of subplots by ~3% and their drawing by ~2% at a reasonably limited cost in legibility. The difference is actually not easy to observe because variation in timings are much greater than 3% from run to run. In a call like `subplots(10, 10)`, just setting up the figure calls `np.identity(3)` 1801 times and actually drawing the figure another 1705 times (measured by manually instrumenting the calls to identity() in the old version. At the same time, ``` %timeit np.identity(3) ``` and ``` %%timeit t = np.identity(3) t.copy() ``` show that on my machine, a call to np.identity(3) is approximately 16us slower than copying a preexisting matrix; multiplying this by 1801 shows that the calls to identity correspond to an excess time of ~27ms. Finally, setting up the axes and drawing it can be timed using ``` from time import perf_counter import matplotlib; matplotlib.use("agg") from matplotlib import pyplot as plt N = 10 # First draw always seems slower, so skip it. figure = plt.figure() figure.subplots(N, N) figure.canvas.draw() plt.close("all") for _ in range(5): figure = plt.figure() start = perf_counter() figure.subplots(N, N) print("{: 4}".format(int(1000 * (perf_counter() - start))), end=" ") start = perf_counter() figure.canvas.draw() print("{: 4}".format(int(1000 * (perf_counter() - start)))) plt.close("all") ``` which shows that (on my machine) setting up the subplots takes ~850ms and drawing them ~1300ms (but again, with a lot of jitter). Hence, the gain from the patch should be ~3% for the setup and ~2% for the draw.
1 parent fde699c commit 6afd28c
Copy full SHA for 6afd28c

File tree

Expand file treeCollapse file tree

1 file changed

+5
-3
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+5
-3
lines changed

‎lib/matplotlib/transforms.py

Copy file name to clipboardExpand all lines: lib/matplotlib/transforms.py
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,8 @@ def __init__(self, matrix=None, **kwargs):
19281928
"""
19291929
Affine2DBase.__init__(self, **kwargs)
19301930
if matrix is None:
1931-
matrix = np.identity(3)
1931+
# A bit faster than np.identity(3).
1932+
matrix = IdentityTransform._mtx.copy()
19321933
self._mtx = matrix
19331934
self._invalid = 0
19341935

@@ -1999,13 +2000,14 @@ def identity():
19992000
Unless this transform will be mutated later on, consider using
20002001
the faster :class:`IdentityTransform` class instead.
20012002
"""
2002-
return Affine2D(np.identity(3))
2003+
return Affine2D()
20032004

20042005
def clear(self):
20052006
"""
20062007
Reset the underlying matrix to the identity transform.
20072008
"""
2008-
self._mtx = np.identity(3)
2009+
# A bit faster than np.identity(3).
2010+
self._mtx = IdentityTransform._mtx.copy()
20092011
self.invalidate()
20102012
return self
20112013

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.