So many posts have been written about string concatenation in python.
I needed to look into this issue myself a few days ago and decided to check.
I wrote a deterministic timer, and ran the following tests:
- concatenate a string with +=
- concatenate bytearray string with extend
- concatenate a list of strings with str.join
- concatenate strings with cStringIO
Here are the results for 1000000 iterations:
$ ITERATIONS=1000000 python profile.py 1> /dev/null |
They are quite surprising. The bytearray += operation speed was expected (bytearray is mutable), but I was expecting cstringio and str.join to outperform an immutable string += operation - specifically after reading this benchmark.
What’s going on here?