Use the cProfile package of python instead of own clumsy code.
authorLukas Tönne <lukas.toenne@gmail.com>
Fri, 12 Dec 2014 23:43:59 +0000 (00:43 +0100)
committerLukas Tönne <lukas.toenne@gmail.com>
Fri, 12 Dec 2014 23:43:59 +0000 (00:43 +0100)
object_physics_meadow/hierarchical_dart_throw.py
object_physics_meadow/meadow.py

index ff0bdb8..2ced12f 100644 (file)
@@ -28,10 +28,6 @@ from object_physics_meadow.util import *
 # "Poisson Disk Point Sets by Hierarchical Dart Throwing"
 # (White, Cline, Egbert)
 
-prof_find_cell = Profiling("find_cell")
-prof_test_coverage = Profiling("test_coverage")
-prof_test_disk = Profiling("test_disk")
-
 class GridCell():
     __slots__ = ('i', 'j', 'k')
     
@@ -153,7 +149,6 @@ class PointGrid():
                     yield p
 
 def is_covered(radius2, b0, pgrid, level, cell_i, cell_j, x0, x1, y0, y1):
-    #with prof_test_coverage:
     cx = 0.5*(x0 + x1)
     cy = 0.5*(y0 + y1)
     for point in pgrid.neighbors(level, cell_i, cell_j):
@@ -166,7 +161,6 @@ def is_covered(radius2, b0, pgrid, level, cell_i, cell_j, x0, x1, y0, y1):
     return False
 
 def test_disk(radius2, pgrid, point, level, cell_i, cell_j):
-    #with prof_test_disk:
     for npoint in pgrid.neighbors(level, cell_i, cell_j):
         dx = point[0] - npoint[0]
         dy = point[1] - npoint[1]
@@ -211,7 +205,6 @@ def hierarchical_dart_throw_gen(radius, max_levels, xmin, xmax, ymin, ymax, debu
             if not any(level.cells for level in levels):
                 break
             
-            #with prof_find_cell:
             level, cell = pop_cell(levels)
             if level:
                 x0, x1, y0, y1, z0, z1 = level.cell_corners(cell)
@@ -230,10 +223,6 @@ def hierarchical_dart_throw_gen(radius, max_levels, xmin, xmax, ymin, ymax, debu
             else:
                 break
     
-        #print(prof_find_cell.as_string())
-        #print(prof_test_coverage.as_string())
-        #print(prof_test_disk.as_string())
-    
     return gen
 
 #-----------------------------------------------------------------------
index 17482fb..087f6a6 100644 (file)
@@ -18,7 +18,7 @@
 
 # <pep8 compliant>
 
-import bpy, os
+import bpy, os, cProfile, pstats, io
 from mathutils import *
 
 from object_physics_meadow import settings as _settings
@@ -30,6 +30,8 @@ from object_physics_meadow.duplimesh import project_on_ground
 from object_physics_meadow.best_candidate import best_candidate_gen
 from object_physics_meadow.hierarchical_dart_throw import hierarchical_dart_throw_gen, MeshDebug
 
+use_profiling = False
+
 def make_samples(context, gridob, groundob):
     settings = _settings.get(context)
     
@@ -48,7 +50,18 @@ def make_samples(context, gridob, groundob):
     gen = hierarchical_dart_throw_gen(groundob.meadow.patch_radius, groundob.meadow.sampling_levels, xmin, xmax, ymin, ymax, debug)
     
     mat = groundob.matrix_world
-    loc2D = [(mat * Vector(p[0:3] + (1.0,)))[0:2] for p in gen(groundob.meadow.seed, groundob.meadow.max_patches)]
+    if use_profiling:
+        prof = cProfile.Profile()
+        prof.enable()
+        loc2D = [(mat * Vector(p[0:3] + (1.0,)))[0:2] for p in gen(groundob.meadow.seed, groundob.meadow.max_patches)]
+        prof.disable()
+
+        s = io.StringIO()
+        ps = pstats.Stats(prof, stream=s).sort_stats('cumulative')
+        ps.print_stats()
+        print(s.getvalue())
+    else:
+        loc2D = [(mat * Vector(p[0:3] + (1.0,)))[0:2] for p in gen(groundob.meadow.seed, groundob.meadow.max_patches)]
     #debug.to_object(context)
     
     # project samples onto the ground object