From 4cc7794b585fcb56c9993869651293d81215a711 Mon Sep 17 00:00:00 2001 From: Yatoom Date: Tue, 6 Apr 2021 21:50:46 +0200 Subject: [PATCH] Added performance profiling example. --- examples/profiling_performance.py | 41 +++++++++++++++++++++++++++++++ voronoi/graph/point.py | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 examples/profiling_performance.py diff --git a/examples/profiling_performance.py b/examples/profiling_performance.py new file mode 100644 index 0000000..78b9b0e --- /dev/null +++ b/examples/profiling_performance.py @@ -0,0 +1,41 @@ +from voronoi import Voronoi, Polygon +import cProfile +import pstats + + +def profiler(command, filename="profile.stats", n_stats=20): + """Profiler for a python program + + Runs cProfile and outputs ordered statistics that describe + how often and for how long various parts of the program are executed. + + Parameters + ---------- + command: str + Command string to be executed. + filename: str + Name under which to store the stats. + n_stats: int or None + Number of top stats to show. + """ + + cProfile.run(command, filename) + stats = pstats.Stats(filename).strip_dirs().sort_stats("cumtime") + return stats.print_stats(n_stats or {}) + + +# Define some points (a.k.a sites or cell points) +points = [ + (2.5, 2.5), (4, 7.5), (7.5, 2.5), (6, 7.5), (4, 4), (3, 3), (6, 3) +] + +# Define a bounding box / polygon +polygon = Polygon([ + (2.5, 10), (5, 10), (10, 5), (10, 2.5), (5, 0), (2.5, 0), (0, 2.5), (0, 5) +]) + +# Initialize the algorithm +v = Voronoi(polygon) + +# Profile the construction of the voronoi diagram +profiler('v.create_diagram(points=points)') diff --git a/voronoi/graph/point.py b/voronoi/graph/point.py index c23008d..4f52ce2 100644 --- a/voronoi/graph/point.py +++ b/voronoi/graph/point.py @@ -16,7 +16,7 @@ def __init__(self, x=None, y=None, name=None, first_edge=None): >>> size: float = site.area() # The area of the cell >>> borders: List[HalfEdge] = site.borders() # Borders around this cell point - >>> vertices: List[Vertex] = site._vertices() # Vertices around this cell point + >>> vertices: List[Vertex] = site.vertices() # Vertices around this cell point >>> site_x: float = site.x # X-coordinate of the site >>> site_xy: [float, float] = site.xy # (x, y)-coordinates of the site >>> first_edge: HalfEdge = site.first_edge # First edge of the site's border