Skip to content

Instantly share code, notes, and snippets.

@queertypes
Created October 3, 2013 15:42
Show Gist options
  • Save queertypes/6811965 to your computer and use it in GitHub Desktop.
Save queertypes/6811965 to your computer and use it in GitHub Desktop.
Microbenchmark of small string concatenation methods in Python 2.7.5, Python 3.3.2, and pypy-2.1.
# Microbenchmark of small string concatenation methods in Python 2.7.5, Python 3.3.2, and pypy-2.1
## Python 2.7.5
In [1]: %timeit '{0}/{1}'.format('taco', 'bell')
1000000 loops, best of 3: 226 ns per loop
In [2]: %timeit '{}/{}'.format('taco', 'bell')
1000000 loops, best of 3: 366 ns per loop
In [3]: %timeit '%s/%s' % ('taco', 'bell')
10000000 loops, best of 3: 174 ns per loop
In [4]: %timeit 'taco' + '/' + 'bell'
10000000 loops, best of 3: 30.4 ns per loop
## Python 3.3.2
In [1]: %timeit '{0}/{1}'.format('taco', 'bell')
1000000 loops, best of 3: 250 ns per loop
In [2]: %timeit '{}/{}'.format('taco', 'bell')
1000000 loops, best of 3: 406 ns per loop
In [3]: %timeit '%s/%s' % ('taco', 'bell')
100000000 loops, best of 3: 18.1 ns per loop
In [4]: %timeit 'taco' + '/' + 'bell'
100000000 loops, best of 3: 18.5 ns per loop
## pypy-2.1
In [1]: %timeit '{0}/{1}'.format('taco', 'bell')
100000000 loops, best of 3: 2.62 ns per loop
In [2]: %timeit '{}/{}'.format('taco', 'bell')
100000000 loops, best of 3: 2.62 ns per loop
In [3]: %timeit '%s/%s' % ('taco', 'bell')
100000000 loops, best of 3: 2.61 ns per loop
In [4]: %timeit 'taco' + '/' + 'bell'
100000000 loops, best of 3: 2.62 ns per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment