1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
# Offers example of inline C for binary search algorithm.
# Borrowed from Kalle Svensson in the Python Cookbook.
# The results are nearly in the "not worth it" category.
#
# C:\home\ej\wrk\scipy\compiler\examples>python binary_search.py
# Binary search for 3000 items in 100000 length list of integers:
# speed in python: 0.139999985695
# speed in c: 0.0900000333786
# speed up: 1.41
# search(a,3450) 3450 3450
# search(a,-1) -1 -1
# search(a,10001) 10001 10001
#
# Note -- really need to differentiate between conversion errors and
# run time errors. This would reduce useless compiles and provide a
# more intelligent control of things.
from __future__ import absolute_import, print_function
import sys
sys.path.insert(0,'..')
# from compiler import inline_tools
import scipy.weave.inline_tools as inline_tools
from bisect import bisect_left as bisect
import types
def c_int_search(seq,t,chk=1):
# do partial type checking in Python.
# checking that list items are ints should happen in py_to_scalar<int>
# if chk:
# assert(type(t) is int)
# assert(type(seq) is list)
code = """
#line 33 "binary_search.py"
if (!PyList_Check(py_seq))
py::fail(PyExc_TypeError, "seq must be a list");
if (!PyInt_Check(py_t))
py::fail(PyExc_TypeError, "t must be an integer");
int val, m, min = 0;
int max = seq.len()- 1;
for(;;)
{
if (max < min )
{
return_val = -1;
break;
}
m = (min + max) / 2;
val = py_to_int(PyList_GET_ITEM(py_seq,m),"val");
if (val < t)
min = m + 1;
else if (val > t)
max = m - 1;
else
{
return_val = m;
break;
}
}
"""
# return inline_tools.inline(code,['seq','t'],compiler='msvc')
return inline_tools.inline(code,['seq','t'],verbose=2)
def c_int_search_scxx(seq,t,chk=1):
# do partial type checking in Python.
# checking that list items are ints should happen in py_to_scalar<int>
if chk:
assert(type(t) is int)
assert(type(seq) is list)
code = """
#line 67 "binary_search.py"
int val, m, min = 0;
int max = seq.len()- 1;
for(;;)
{
if (max < min )
{
return_val = -1;
break;
}
m = (min + max) / 2;
val = seq[m];
if (val < t)
min = m + 1;
else if (val > t)
max = m - 1;
else
{
return_val = m;
break;
}
}
"""
# return inline_tools.inline(code,['seq','t'],compiler='msvc')
return inline_tools.inline(code,['seq','t'],verbose=2)
try:
from numpy import *
def c_array_int_search(seq,t):
code = """
#line 62 "binary_search.py"
int val, m, min = 0;
int max = Nseq[0] - 1;
PyObject *py_val;
for(;;)
{
if (max < min )
{
return_val = -1;
break;
}
m = (min + max) / 2;
val = seq[m];
if (val < t)
min = m + 1;
else if (val > t)
max = m - 1;
else
{
return_val = m;
break;
}
}
"""
# return inline_tools.inline(code,['seq','t'],compiler='msvc')
return inline_tools.inline(code,['seq','t'],verbose=2,
extra_compile_args=['-O2','-G6'])
except:
pass
def py_int_search(seq, t):
min = 0
max = len(seq) - 1
while 1:
if max < min:
return -1
m = (min + max) / 2
if seq[m] < t:
min = m + 1
elif seq[m] > t:
max = m - 1
else:
return m
import time
def search_compare(a,n):
print('Binary search for %d items in %d length list of integers:' % (n,m))
t1 = time.time()
for i in range(n):
py_int_search(a,i)
t2 = time.time()
py = (t2-t1)
print(' speed in python:', (t2 - t1))
# bisect
t1 = time.time()
for i in range(n):
bisect(a,i)
t2 = time.time()
bi = (t2-t1) + 1e-20 # protect against div by zero
print(' speed of bisect:', bi)
print(' speed up: %3.2f' % (py/bi))
# get it in cache
c_int_search(a,i)
t1 = time.time()
for i in range(n):
c_int_search(a,i,chk=1)
t2 = time.time()
sp = (t2-t1)+1e-20 # protect against div by zero
print(' speed in c:',sp)
print(' speed up: %3.2f' % (py/sp))
# get it in cache
c_int_search(a,i)
t1 = time.time()
for i in range(n):
c_int_search(a,i,chk=0)
t2 = time.time()
sp = (t2-t1)+1e-20 # protect against div by zero
print(' speed in c(no asserts):',sp)
print(' speed up: %3.2f' % (py/sp))
# get it in cache
c_int_search_scxx(a,i)
t1 = time.time()
for i in range(n):
c_int_search_scxx(a,i,chk=1)
t2 = time.time()
sp = (t2-t1)+1e-20 # protect against div by zero
print(' speed for scxx:',sp)
print(' speed up: %3.2f' % (py/sp))
# get it in cache
c_int_search_scxx(a,i)
t1 = time.time()
for i in range(n):
c_int_search_scxx(a,i,chk=0)
t2 = time.time()
sp = (t2-t1)+1e-20 # protect against div by zero
print(' speed for scxx(no asserts):',sp)
print(' speed up: %3.2f' % (py/sp))
# get it in cache
a = array(a)
try:
a = array(a)
c_array_int_search(a,i)
t1 = time.time()
for i in range(n):
c_array_int_search(a,i)
t2 = time.time()
sp = (t2-t1)+1e-20 # protect against div by zero
print(' speed in c(numpy arrays):',sp)
print(' speed up: %3.2f' % (py/sp))
except:
pass
if __name__ == "__main__":
# note bisect returns index+1 compared to other algorithms
m = 100000
a = range(m)
n = 50000
search_compare(a,n)
print('search(a,3450)', c_int_search(a,3450), py_int_search(a,3450), bisect(a,3450))
print('search(a,-1)', c_int_search(a,-1), py_int_search(a,-1), bisect(a,-1))
print('search(a,10001)', c_int_search(a,10001), py_int_search(a,10001),bisect(a,10001))
|