Advertisement
Guest User

Untitled

a guest
Dec 27th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. In [106]: def func1(mystr,sub,k):
  2.     inds = (i for i,c in enumerate(reversed(mystr)) if c==sub)
  3.     return len(mystr)-tuple(islice(inds,k-1,k))[-1]-1
  4.    .....:
  5.  
  6. In [107]: def func2(mystr,sub,k):
  7.     return [i for i,c in enumerate(mystr) if c==sub][-k]
  8.    .....:
  9.  
  10. In [108]: strs="abcdabababcebc"*10000
  11.  
  12. In [109]: %timeit func1(strs,'b',200)
  13. 10000 loops, best of 3: 129 us per loop
  14.  
  15. In [110]: %timeit func2(strs,'b',200)
  16. 10 loops, best of 3: 28.4 ms per loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement