Rémi Lapeyre <remi.lape...@henki.fr> added the comment: Hi Wansoo, using += instead of str.join() is less performant. Concatenating n strings with + will create and allocate n new strings will str.join() will carefully look ahead and allocate the correct amount of memory and do all concatenation at one:
➜ ~ python3 -m timeit -s 's = ""' 'for i in range(1_000_000): s += "foo\n"' 5 loops, best of 5: 107 msec per loop ➜ ~ python3 -m timeit -s 'l = ["foo"]*1_000_000' '"\n".join(l)' 20 loops, best of 5: 9.96 msec per loop It's a common idiom that you will meet a lot in Python. ---------- nosy: +remi.lapeyre _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue41242> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com