In this SO post, https://softwareengineering.stackexchange.com/questions/304445/why-is-s-better-than-for-concatenation, it is said that %s is better than +. but my tests show the opposite.
import string
import random
R = 10
def doit1():
x =""
for _ in range(R):
x += random.choice(string.letters)
def doit2():
y = ""
for _ in range(R):
y = "%s%s" % (y, random.choice(string.letters))
if __name__ == '__main__':
import timeit
print(timeit.timeit("doit1()", setup="from __main__ import doit1"))
print(timeit.timeit("doit2()", setup="from __main__ import doit2"))
is giving me the below output:
➜ Documents python3.5 string_test.py
30.200247984997986
33.85921495900038
Seems like they are almost the same. Am I missing something here?