ideasman42 / isect_segments-bentley_ottmann

BentleyOttmann sweep-line implementation (for finding all intersections in a set of line segments)
MIT License
91 stars 40 forks source link

Avoid needlessly calling str() when raising KeyError. #23

Closed anntzer closed 3 years ago

anntzer commented 3 years ago

Removing the call to __str__ speeds up the following example (admittedly slightly aritificial) by ~5%, with no disadvantage (exceptions will automatically call repr on their argument if a traceback actually needs to be displayed):

from poly_point_isect import *
import random
from timeit import Timer

random.seed(1)
points = [(random.random(), random.random()) for _ in range(200)]
n, t = Timer("isect_polygon(points)", globals=globals()).autorange()
print(t/n)
anntzer commented 3 years ago

Feel free to take over the patch and adapt it in whatever way seems useful to you, or ignore it.

ideasman42 commented 3 years ago

Committed alternate optimization 3ca63b028f979009c8505f58d7481a4863c6b246