Saturday, 24 August 2013

Why is list element replacement slower than string element replacement in python?

Why is list element replacement slower than string element replacement in
python?

I am trying to replace a set of elements in a data-structure to some other
value. It seems that doing such kind of replacement is significantly
faster in strings than in lists in case of python (as revealed by the
benchmarking tests below). Can someone please explain why.
Note: These tests were performed on using python 2.7.
def string_replace_test(s, chars):
"""Replaces a set of chars to 0"""
new = s
for c in chars:
new = new.replace(c, '0')
return new
def list_replace_test(s, chars):
"""Replaces a set of chars to 0"""
for a 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 at neque consectetur
at 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"))

No comments:

Post a Comment