Skip to content

Commit

Permalink
Added performance profiling example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatoom committed Apr 6, 2021
1 parent 51335d4 commit 4cc7794
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
41 changes: 41 additions & 0 deletions examples/profiling_performance.py
Original file line number Diff line number Diff line change
@@ -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)')
2 changes: 1 addition & 1 deletion voronoi/graph/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4cc7794

Please sign in to comment.