Skip to content

Instantly share code, notes, and snippets.

@bittner
Created August 16, 2016 07:36
Show Gist options
  • Save bittner/d105bf7e1d75712d812ac89837df8f59 to your computer and use it in GitHub Desktop.
Save bittner/d105bf7e1d75712d812ac89837df8f59 to your computer and use it in GitHub Desktop.
# Python string concatenation
# Appending strings to an existing string is way faster than prepending.
# Source: https://groups.google.com/d/msg/comp.lang.python/AzYJ0LAWe-w/wBLtn5BJIBsJ
python -m timeit -s "v = 'x' * 10; out = ''" "out = out + v"
# prints: 10000000 loops, best of 3: 0.0511 usec per loop
python -m timeit -s "v = 'x' * 10; out = ''" "out = v + out"
# prints: 100000 loops, best of 3: 52.7 usec per loop
python -m timeit -s "v = 'x' * 10; out = ''" "out += v"
# prints: 10000000 loops, best of 3: 0.053 usec per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment