Skip to content

Instantly share code, notes, and snippets.

@sumeet
Created December 1, 2010 08:05
Show Gist options
  • Save sumeet/723145 to your computer and use it in GitHub Desktop.
Save sumeet/723145 to your computer and use it in GitHub Desktop.
String concatenation in CPython 2.7 and PyPy 1.4
(12:01a) (sumeet@shanoa:~)$ `which python2.7` ./timethese.py string_concat.py 10000000
# Running code block #1
assert ''.join(('abc', 'def', 'ghi')) == 'abcdefghi'
Took 2.855692 seconds to run 10000000 times.
# Running code block #2
assert 'abc' + 'def' + 'ghi' == 'abcdefghi'
Took 0.762806 seconds to run 10000000 times.
# Running code block #3
assert '%s%s%s' % ('abc', 'def', 'ghi') == 'abcdefghi'
Took 3.625320 seconds to run 10000000 times.
(12:01a) (sumeet@shanoa:~)$ `which pypy` ./timethese.py string_concat.py 10000000
# Running code block #1
assert ''.join(('abc', 'def', 'ghi')) == 'abcdefghi'
Took 2.484319 seconds to run 10000000 times.
# Running code block #2
assert 'abc' + 'def' + 'ghi' == 'abcdefghi'
Took 0.034994 seconds to run 10000000 times.
# Running code block #3
assert '%s%s%s' % ('abc', 'def', 'ghi') == 'abcdefghi'
Took 0.034827 seconds to run 10000000 times.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment