python - String concatenation performance -


right use list store altered string , return string .join()

def applycoder(text, coder):     l = []     in text:         if in coder:             l.append(coder[i])         else:             l.append(i)     return ''.join(l)  # example output, shifts letters 3 in string # print applycoder("hello, world!", buildcoder(3)) # buildcoder(3) returns dictionary, e.g. {'a': 'd', ...} # >>> khoor, zruog! 

is there faster method change , return string?

this should fast possible:

''.join([coder[i] if in coder else in text]) 

list comprehensions more optimized in python compared for loops bear great overhead. i've passed list comprehension opposed generator in ''.join because must know length of it's input in advance before joining. if give generator has make list anyway (which bit slower).

actually can simplify further, which should faster (this performs slower above method due method call)

''.join([coder.get(i,i) in text]) 

timings:

def applycoder(text, coder):     l = []     in text:         if in coder:             l.append(coder[i])         else:             l.append(i)     return ''.join(l)  def list_comp(text, coder):     return ''.join([coder[i] if in coder else in text])  def list_comp2(text, coder):     return ''.join([coder.get(i,i) in text])  timeit import timeit string import ascii_letters d = dict(zip(ascii_letters, ascii_letters[3:] + ascii_letters[-3:]))   print timeit(stmt='applycoder("hello, world!", d)',              setup='from __main__ import applycoder, d;')  print timeit(stmt='list_comp("hello, world!", d)',              setup='from __main__ import list_comp, d;')      print timeit(stmt='list_comp2("hello, world!", d)',              setup='from __main__ import list_comp2, d;')  print timeit(stmt='applycoder("hello, world!"*10, d)',              setup='from __main__ import applycoder, d;')  print timeit(stmt='list_comp("hello, world!"*10, d)',              setup='from __main__ import list_comp, d;')   print timeit(stmt='list_comp2("hello, world!"*10, d)',              setup='from __main__ import list_comp2, d;') 

results:

''' test 1 ''' 5.0159105417    # applycoder 3.41502481461   # listcomp1 4.76796932292   # listcomp2  ''' test 2 ''' 34.9718502631   # applycoder 22.0451702661   # listcomp1 34.1682597928   # listcomp2 

it appears method call coder.get negates advantages of list comprehension. did predict might slower listcomp1 because of didn't think have of impact. anyway list comprehension still wins.

update: if modify list_comp2 so:

def list_comp2(text, coder):     coder_get = coder.get     return ''.join([coder_get(i,i) in text]) 

the times improve drastically:

from 4.76796932292 (1st test) -> 3.95217394948

and 34.1682597928 (2nd test) -> 27.1162974624


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -