I'm trying to improve some code in order to get a better perfomance, I have to do a lot of pattern matching for little tags on medium large strings, for example:
import re
STR = "0001x10x11716506872xdc23654&xd01371600031832xx10xID=000110011001010\n"
def getID(string):
result = re.search('.*ID=(\d+)', string)
if result:
print result.group(1)
else:
print "No Results"
Taking in mind the perfomance I rewrite it:
def getID2(string):
result = string.find('ID=')
if result:
result += 3
print string[result:result+15]
else:
print 'No Results'
Are there a better way to improve these approaches? Any comments are welcome...
print
will be more expensive then anything else and relative to that no other change will really matter. – Winston Ewert♦ Jan 17 at 15:17self
andreturn
ing the answer. If you want help you need to show your actual code! As it stands this is totally useless. – Winston Ewert♦ Jan 17 at 16:44