import time
def concat(n):
start = time.time()
s = ""
for i in range(n):
s += "Вася" + str(i)
end = time.time()
print("%d strings concatenated in %.3f seconds" % (n, end - start))
def join(n):
start = time.time()
def gen(n):
for i in range(n):
yield "Вася" + str(i)
s = "".join(gen(n))
end = time.time()
print("%d strings joined in %.3f seconds" % (n, end - start))
concat(3000000)
join(3000000)
aW1wb3J0IHRpbWUKCmRlZiBjb25jYXQobik6CglzdGFydCA9IHRpbWUudGltZSgpCglzID0gIiIKCWZvciBpIGluIHJhbmdlKG4pOgoJCXMgKz0gItCS0LDRgdGPIiArIHN0cihpKQoJZW5kID0gdGltZS50aW1lKCkKCXByaW50KCIlZCBzdHJpbmdzIGNvbmNhdGVuYXRlZCBpbiAlLjNmIHNlY29uZHMiICUgKG4sIGVuZCAtIHN0YXJ0KSkKCQpkZWYgam9pbihuKToKCXN0YXJ0ID0gdGltZS50aW1lKCkKCQoJZGVmIGdlbihuKToKCQlmb3IgaSBpbiByYW5nZShuKToKCQkJeWllbGQgItCS0LDRgdGPIiArIHN0cihpKQoJCQkKCXMgPSAiIi5qb2luKGdlbihuKSkKCWVuZCA9IHRpbWUudGltZSgpCglwcmludCgiJWQgc3RyaW5ncyBqb2luZWQgaW4gJS4zZiBzZWNvbmRzIiAlIChuLCBlbmQgLSBzdGFydCkpCgoKCmNvbmNhdCgzMDAwMDAwKQpqb2luKDMwMDAwMDAp