Why is list element replacement slower than string element replacement in python? -
i trying replace set of elements in data-structure other value. seems doing such kind of replacement faster in strings in lists in case of python (as revealed benchmarking tests below). can please explain why.
note: these tests performed on using python 2.7.
def string_replace_test(s, chars): """replaces set of chars 0""" new = s c in chars: new = new.replace(c, '0') return new def list_replace_test(s, chars): """replaces set of chars 0""" in xrange(len(s)): if s[a] in chars: s[a] = '0' if __name__ == '__main__': import timeit s = """ lorem ipsum dolor sit amet, consectetur adipiscing elit. donec etfringilla purus. pellentesque bibendum urna @ neque consectetur @ tincidunt nulla luctus. pellentesque augue lacus, interdum id lectus vitae, laoreet suscipit arcu. """ s2 = list(s) chars = ['a', 'e', 'i', 'o', 'u'] print(timeit.timeit("string_replace_test(s, chars)", setup="from __main__ import string_replace_test, s, chars")) print(timeit.timeit("list_replace_test(s2, chars)", setup="from __main__ import list_replace_test, s2, chars"))
output:
5.09572291374 49.3243050575
using range():
5.01253795624 53.2320859432
since there no list.replace()
function, built own, picked slow method.
use list comprehension instead:
def list_replace_test(s, chars): """replaces set of chars 0""" return [a if not in chars else '0' in s]
this still slower string replacement because cannot avoid python loop here.
using set chars
helps:
chars = set(chars)
but fastest way replace individual characters in text different technique altogether. use str.translate()
that:
from string import maketrans map = maketrans('aeiou', '0' * 5) def str_translate(s, map): return s.translate(map)
with these changes timings become:
>>> timeit.timeit("list_replace_test(s2, chars)", setup="from __main__ import list_replace_test, s2, chars") 28.60542392730713 >>> timeit.timeit("string_replace_test(s, chars)", setup="from __main__ import string_replace_test, s, chars") 4.002871990203857 >>> timeit.timeit("str_translate(s, map)", setup="from __main__ import str_translate, s, map") 0.7250571250915527
blowing looped str.replace()
calls right out of water too.
Comments
Post a Comment