Search This Blog

Sunday, April 17, 2016

which would be faster on python?

I wanted to know which would be faster to find a small string within a LARGE one, regular expression or the find function (intuitively I assumed find would be faster, but the size of the Large string bothered me, and I wished to be on the sfe side). 

On the way ran into the more general question: how to check which is faster in python? 

The answer: the timeit module - a simple way to time small bits of Python code.
An example: 
import timeit
import re
string1="aaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaa"
string2="baaa"
print ('find='+str(timeit.timeit("string1.find(string2)",'string1="aaaaaaaaaaaabbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaa"; string2="baaa"')))
print('re='+str(timeit.timeit("re.match(string1, string2)","from __main__ import string1,string2,re")))
One has to admit that it isn't the most convenient method, but still - the answer on my machine, for a single run (which is better than none, but more times would be better), was: 

$ python3 testimeit.py
find=0.31890193399158306
re=0.8841714099980891

Further reading                        
  • a discussion in stackoverflow  
  • the docs: Worthwhile knowing that timeit can be used from the Command-Line Interface as well as in the callable form. Also worth checking the examples in the docs - timeit can be used in  more readable than this post may make one fear... 



No comments:

Post a Comment