Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b65fa1

Browse files
committed
Speed up Path.iter_segments()
Using the Gtk3Cairo backend with wire3d_animation_sgskip.py: Before: 9.16 fps After: 9.95 fps The main speedup is from iterating and keeping the common non-curve case simple.
1 parent cdc3ab1 commit 8b65fa1

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

lib/matplotlib/path.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -399,24 +399,22 @@ def iter_segments(self, transform=None, remove_nans=True, clip=None,
399399
snap=snap, stroke_width=stroke_width,
400400
simplify=simplify, curves=curves,
401401
sketch=sketch)
402-
vertices = cleaned.vertices
403-
codes = cleaned.codes
404-
len_vertices = vertices.shape[0]
405402

406403
# Cache these object lookups for performance in the loop.
407404
NUM_VERTICES_FOR_CODE = self.NUM_VERTICES_FOR_CODE
408405
STOP = self.STOP
409406

410-
i = 0
411-
while i < len_vertices:
412-
code = codes[i]
407+
vertices = iter(cleaned.vertices)
408+
codes = iter(cleaned.codes)
409+
for curr_vertices, code in zip(vertices, codes):
413410
if code == STOP:
414-
return
415-
else:
416-
num_vertices = NUM_VERTICES_FOR_CODE[code]
417-
curr_vertices = vertices[i:i+num_vertices].flatten()
418-
yield curr_vertices, code
419-
i += num_vertices
411+
break
412+
extra_vertices = NUM_VERTICES_FOR_CODE[code] - 1
413+
if extra_vertices:
414+
for i in range(extra_vertices):
415+
next(codes)
416+
curr_vertices = np.append(curr_vertices, next(vertices))
417+
yield curr_vertices, code
420418

421419
def cleaned(self, transform=None, remove_nans=False, clip=None,
422420
quantize=False, simplify=False, curves=False,

0 commit comments

Comments
 (0)