[Bf-extensions-cvs] [01e6f1b] master: Squeeze a little bit more performance out of the sampling code by "pre-allocating" grid lists.

Lukas Tönne noreply at git.blender.org
Mon Dec 15 14:31:36 CET 2014


Commit: 01e6f1b638102541c737657c4098d41b0b6f34a6
Author: Lukas Tönne
Date:   Mon Dec 15 12:08:08 2014 +0100
Branches: master
https://developer.blender.org/rBAC01e6f1b638102541c737657c4098d41b0b6f34a6

Squeeze a little bit more performance out of the sampling code by
"pre-allocating" grid lists.

This is about as far as optimization can go in plain python (avoiding
class __init__ methods could help further). Eventually this code should
be implemented in C/C++ though, dealing with large arrays is not pythons
strong point ...

===================================================================

M	object_physics_meadow/hierarchical_dart_throw.py

===================================================================

diff --git a/object_physics_meadow/hierarchical_dart_throw.py b/object_physics_meadow/hierarchical_dart_throw.py
index 9b0868c..77b84f1 100644
--- a/object_physics_meadow/hierarchical_dart_throw.py
+++ b/object_physics_meadow/hierarchical_dart_throw.py
@@ -49,11 +49,18 @@ class GridLevel():
     def activate(self, i, j, k):
         cell = GridCell(i, j, k)
         self.cells.append(cell)
-        
-        x0, x1, y0, y1, _, _ = self.cell_corners(cell)
-        
         return cell
 
+    def set_active_cells(self, imin, imax, jmin, jmax, kmin, kmax):
+        tot = (imax - imin) * (jmax - jmin) * (kmax - kmin)
+        self.cells = [None] * tot
+        c = 0
+        for k in range(kmin, kmax):
+            for j in range(jmin, jmax):
+                for i in range(imin, imax):
+                    self.cells[c] = GridCell(i, j, k)
+                    c += 1
+
     def deactivate(self, index):
         c = self.cells[index]
         if index < len(self.cells)-1:
@@ -225,9 +232,7 @@ def hierarchical_dart_throw_gen(radius, max_levels, xmin, xmax, ymin, ymax):
         levels = [base_level] + [GridLevel(i, base_level.size / (2**i), radius) for i in range(1, max_levels)]
         epsilon = levels[-1].weight * 0.5
         
-        for j in range(jmin, jmax):
-            for i in range(imin, imax):
-                base_level.activate(i, j, 0)
+        base_level.set_active_cells(imin, imax, jmin, jmax, 0, 1)
         
         pgrid = PointGrid(radius, b0, gridmin, gridmax)



More information about the Bf-extensions-cvs mailing list