From 56a5cadde2e81788b89c6a68e97395554ebdc374 Mon Sep 17 00:00:00 2001 From: Zachary Pincus Date: Thu, 8 Apr 2010 18:11:47 -0400 Subject: [PATCH 1/2] Performance fix for heap; correctness for _mcp (1) normalize_indices in _mcp.pyx should convert indices to ints: fixed (2) huge (order of magnitude) performance problems in heap.pyx from changing lines like: ii = (i-1)/2 # CalcPrevAbs to: ii = (int)(i-1)/2 # CalcPrevAbs for some reason, the latter goes through the python division machinery! However, the former is invalid for cython >=0.12, so the proper solution is: ii = (i-1)//2 # CalcPrevAbs --- scikits/image/graph/_mcp.pyx | 1 + scikits/image/graph/heap.pyx | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/scikits/image/graph/_mcp.pyx b/scikits/image/graph/_mcp.pyx index d56fee64..c1a6429e 100644 --- a/scikits/image/graph/_mcp.pyx +++ b/scikits/image/graph/_mcp.pyx @@ -184,6 +184,7 @@ def _normalize_indices(indices, shape): return None new_index = [] for i, s in zip(index, shape): + i = int(i) if i < 0: i = s+i if not (0 <= i < s): diff --git a/scikits/image/graph/heap.pyx b/scikits/image/graph/heap.pyx index 0e31ea8a..fda8c64e 100644 --- a/scikits/image/graph/heap.pyx +++ b/scikits/image/graph/heap.pyx @@ -246,7 +246,7 @@ cdef class BinaryHeap: i0 = (1 << level) - 1 #2**level-1 = LevelStart n = i0 + 1 #2**level for i in range(i0,i0+n,2): - ii = (int)(i-1)/2 # CalcPrevAbs + ii = (i-1)/2 # CalcPrevAbs if values[i] < values[i+1]: values[ii] = values[i] else: @@ -266,7 +266,7 @@ cdef class BinaryHeap: # track tree cdef int ii, level for level in range(self.levels,1,-1): - ii = (int)(i-1)/2 # CalcPrevAbs + ii = (i-1)/2 # CalcPrevAbs # test if values[i] < values[i+1]: values[ii] = values[i] From 4a0e4e2285bfcb45447e9c848e39bb3ddfca3620 Mon Sep 17 00:00:00 2001 From: Zachary Pincus Date: Thu, 8 Apr 2010 18:13:45 -0400 Subject: [PATCH 2/2] Oops: actually fix heap performance problem... --- scikits/image/graph/heap.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scikits/image/graph/heap.pyx b/scikits/image/graph/heap.pyx index fda8c64e..524dd633 100644 --- a/scikits/image/graph/heap.pyx +++ b/scikits/image/graph/heap.pyx @@ -246,7 +246,7 @@ cdef class BinaryHeap: i0 = (1 << level) - 1 #2**level-1 = LevelStart n = i0 + 1 #2**level for i in range(i0,i0+n,2): - ii = (i-1)/2 # CalcPrevAbs + ii = (i-1)//2 # CalcPrevAbs if values[i] < values[i+1]: values[ii] = values[i] else: @@ -266,7 +266,7 @@ cdef class BinaryHeap: # track tree cdef int ii, level for level in range(self.levels,1,-1): - ii = (i-1)/2 # CalcPrevAbs + ii = (i-1)//2 # CalcPrevAbs # test if values[i] < values[i+1]: values[ii] = values[i]