6.5. Case Study: Str Concat

6.5.1. Code

>>> from time import time
>>>
>>>
>>> class Timeit:
...     def __init__(self, name):
...         self.name = name
...
...     def __enter__(self):
...         self.start = time()
...         return self
...
...     def __exit__(self, *arg):
...         end = time()
...         print(f'Duration of {self.name} is {end-self.start:.2f} second')
>>>
>>>
>>> a = 1
>>> b = 2
>>> repetitions = int(10)  # int(1e7)

6.5.2. F-String

>>> with Timeit('f-string'):
...     for _ in range(repetitions):
...         f'{a}{b}'  
Duration of f-string is 2.70 second

6.5.3. Add

>>> with Timeit('string concat'):
...     for _ in range(repetitions):
...         a + b  
Duration of string concat is 0.68 second

6.5.4. Format

>>> with Timeit('str.format()'):
...     for _ in range(repetitions):
...         '{0}{1}'.format(a, b)  
Duration of str.format() is 3.46 second
>>> with Timeit('str.format()'):
...     for _ in range(repetitions):
...         '{}{}'.format(a, b)  
Duration of str.format() is 3.37 second
>>> with Timeit('str.format()'):
...     for _ in range(repetitions):
...         '{a}{b}'.format(a=a, b=b)  
Duration of str.format() is 4.85 second

6.5.5. %-style

>>> with Timeit('%-style'):
...     for _ in range(repetitions):
...         '%s%s' % (a, b)  
Duration of %-style is 2.59 second
>>> with Timeit('%-style'):
...     for _ in range(repetitions):
...         '%d%d' % (a, b)  
Duration of %-style is 2.59 second
>>> with Timeit('%-style'):
...     for _ in range(repetitions):
...         '%f%f' % (a, b)  
Duration of %-style is 3.82 second