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
| from time import clock
def trv_find(ch, car):
return ch.find(car)
def trv_split(ch, car):
try:
start,end = ch.split(car,1)
return len(start)
except:
return -1
def trv_xrange(ch, car):
for i in xrange(len(ch)):
if ch[i] == car: return i
return -1
def trv_nue(ch, car, cnt=-1):
for c in ch:
cnt = cnt + 1
if c==car: return cnt
return -1
def trv_enum(ch, car):
for i,c in enumerate(ch):
if c == car: return i
return -1
def trv_len(ch, car, rec=''):
for c in ch:
rec = rec + c
if c==car: return len(rec)-1
return -1
def trv_recurs(ch, car, z=0):
if z<len(ch)-1 and ch[z]!=car: return trv_recurs(ch,car,z+1)
if z<len(ch) and ch[z]==car: return z
return -1
for name in ('trv_find', 'trv_split', 'trv_xrange',
'trv_nue', 'trv_enum', 'trv_len', 'trv_recurs'):
f = globals()[name]
print name.rjust(12),' ',
for ch in ('phenylglycyl-cysteinyl-tyrosine','fleur',''):
print f(ch,'y'),'\t',
print
print
iterat = 1000
for i in xrange(iterat):
li = []
te = clock()
for j in xrange(100):
x = 'jhehcweifiweggichbeidgcieyfchbhcgeyddgceygqhwb'.find('q') # ....qhwb
tf = clock()
li.append(tf-te)
print 'find'.rjust(12)+' '+str(min(li)).ljust(20)+str(x)
for name in ('trv_find', 'trv_split', 'trv_xrange',
'trv_nue', 'trv_enum', 'trv_len', 'trv_recurs'):
f,li = globals()[name],[]
for i in xrange(iterat):
te = clock()
for j in xrange(100):
x = f('jhehcweifiweggichbeidgcieyfchbhcgeyddgceygqhwb','q') # ....qhwb
tf = clock()
li.append(tf-te)
print name.rjust(12)+' '+str(min(li)).ljust(20)+str(x) |
Partager