string.join is abysmally slow

Darrell news at dorb.com
Sun Apr 15 19:21:08 EDT 2001


### The name list is a built-in, avoid using such names
joinList = []

for line in buf.split("\012"):
    # Comment or blank line?
    if line == '' or line[0] in '#':
        continue
    else:
        joinList.append(string.strip(line))

##### Move the following out of the loop

# "address1|address2|address3|addressN"
regex = string.join(joinList,'|')
regex = '"' + regex + '"'     #<-- Are you matching the quotes ?
reo = re.compile(regex, re.I)

################ Another approach

import re, string
buf="""
# xxxx

re1

re2
#
"""
sep=")|("
buf = re.sub("\012+",sep, re.sub("#.*", "",buf) )
breakOff = len(sep)-1
print buf[breakOff:-breakOff]

"""output --> (re1)|(re2)"""

--Darrell

"Graham Guttocks" wrote:
>
> I've run into a performance problem in one of my functions, and wonder
> if I could get some recommendations on how to speed things up.
>






More information about the Python-list mailing list