Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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...

share|improve this question
2  
Is this the actual code that you are trying to improve the performance of? As it stands, print will be more expensive then anything else and relative to that no other change will really matter. – Winston Ewert Jan 17 at 15:17
This a getter from a class that get the string at the constructor – Christian_Espinoza Jan 17 at 15:54
If its a getter you should be referencing self and returning 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
Also, what performance improvement do you need? What are possible use cases? There's not much more you can do since find("ID=") is O(n), n being the length of the string. You can get performance only by not traversing the whole string, which is not possible unless we know more about your date (ID always at the end?). – Quentin Pradet May 3 at 8:41

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.