Created
March 17, 2017 13:35
-
-
Save tux-00/fb2f753d1afeba3097c46c39e49679ee to your computer and use it in GitHub Desktop.
Test python3.6 string concatenation performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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