Beginner's performance problem

Bengt Richter bokr at oz.net
Thu Apr 11 18:02:12 EDT 2002


On Thu, 11 Apr 2002 14:32 +0100 (BST), mark.charsley at REMOVE_THIS.radioscape.com (Mark Charsley) wrote:

>In article <slrnab68mh.15p.gerhard at lilith.my-fqdn.de>, gerhard at bigfoot.de 
>(Gerhard =?iso-8859-15?Q?H=E4ring?=) wrote:
>
>> I haven't tried but an approach using the re module should be really
>> fast, and shorter. Untested code follows:
>> 
>> import re
>> names = ["ALGORITHM_CLASS", ...]
>> 
>> text = open("ThirteenThousandLines.sql").read()
>> for name in names:
>>     regex = re.compile(name, re.I) # case insensitive search for name
>>     text = regex.sub(name, text)   # replace all occurences
>> print text
>
>Aha! I'd put off reading up on the regular expression stuff until later as 
>I thought it just did searches, as opposed to also doing in-place 
>replaces. Half an hour spent on the HOW-TO and I'm a lot better informed. 
>Add a couple of r'\b' to the above, and I ought to be laughing. 
>
>TVM to everyone who helped.
>
Best reread Alex's post carefully, until you understand the difference from the above ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list