# File: stringTest2.py # # Even if you add a character to the back of a string, # if you convince Python that you still need a copy of the # old string, the + operation is pretty slow. import time import random REPS = 400000 #REPS = 10 random.seed() aString = "" start = time.time() for i in range (0, REPS): newLetter1= chr(random.randint(32,126)) newLetter2 = chr(random.randint(32,126)) # print("newLetter1 =", newLetter1) # print("newLetter2 =", newLetter2) bString = aString + newLetter1 aString = bString + newLetter2 # print("aString =", aString) # print("bString =", bString) finish = time.time() print("Elapsed time =", finish - start, "seconds")