Skip to content

Instantly share code, notes, and snippets.

@tux-00
Created March 17, 2017 13:35
Show Gist options
  • Save tux-00/fb2f753d1afeba3097c46c39e49679ee to your computer and use it in GitHub Desktop.
Save tux-00/fb2f753d1afeba3097c46c39e49679ee to your computer and use it in GitHub Desktop.
Test python3.6 string concatenation performance
def test1():
g = 'gg'
s = 'toto' + g
def test2():
g = 'gg'
s = 'toto%s' % g
def test3():
g = 'gg'
s = 'toto{}'.format(g)
def test4():
g = 'gg'
s = f"toto{g}"
if __name__ == '__main__':
import timeit
print(timeit.timeit("test1()", setup="from __main__ import test1", number=1000000))
print(timeit.timeit("test2()", setup="from __main__ import test2", number=1000000))
print(timeit.timeit("test3()", setup="from __main__ import test3", number=1000000))
print(timeit.timeit("test4()", setup="from __main__ import test4", number=1000000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment