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
This commit is contained in:
Zachary Pincus
2010-04-08 18:11:47 -04:00
parent bc8ab59966
commit 56a5cadde2
2 changed files with 3 additions and 2 deletions
+1
View File
@@ -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):
+2 -2
View File
@@ -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]