Search code examples
pythonf-string

Performance of f-string vs string.join


one = 'one'
two = 'two'
three = 'three'

Are the 2 equivalent in time complexity?


result_1 = f'{one}{two}{three}'
result_2 = ''.join([one, two, three])

I know that join works in linear time. But is the same true also for f-strings? With f-strings, the number of strings being concatenated is always a constant, so I guess one could say it's constant time complexity, but I was more curious to know how the concatenation is implemented and how it would fare with a large number of strings.


Solution

  • The performance of these two approaches may vary between releases of Python and could also depend on your hardware.

    In Python 3.11.3 on macOS 13.4 (3 GHz 10-Core Intel Xeon W) the f-string implementation is faster than str.join()

    from timeit import timeit
    
    ONE = 'one'
    TWO = 'two'
    THREE = 'three'
    
    def func1(one, two, three):
        return f'{one}{two}{three}'
    
    def func2(one, two, three):
        return ''.join([one,two,three])
    
    for func in func1, func2:
        print(func.__name__, timeit(lambda: func(ONE, TWO, THREE), number=2_000_000))
    

    Output:

    func1 0.2694316829999934
    func2 0.33222394099993835
    

    Note:

    This result will be due in part to the fact that func2 has to construct a list. This can be eliminated by re-writing it as:

    def func2(*args):
        return ''.join(args)
    

    ...which timeit reports as:

    func2 0.30138257099997645