why python f-strings are fast

실리콘·2023년 6월 13일
0

Conclusion: f-strings are fastest in CPython because it builds the strings straight from stack to improve performance. A new Opcode BUILD_STRING was added specifically to solve this problem.

there are many ways to format a string in python

  • String concatenation.
    • "My name is " + my_name + "."
  • % format
  • "My name is %s.".format(my_name)
  • "".join(["My name is " , my_name , "."])
  • f-strings (string literals)
    • f"My name is {my_name}."

We can think about some aspects to consider when choosing which method to use.

First,need to decide which implementation are we using? CPython? PyPI? Rust-Python? CPython is most used, so talking about that.

Then,

  • Usability. easy to make mistakes? easy to write code?
  • Performance.
    • Speed. How fast is it for large strings or when it is repeated?
    • Memory usage. How many new strings, i.e. PyObjects

This blog is about performance part. For Usability you can refer to many good posts, or realpython.org, or google python guide.

Most performance issue in CPython is due to creation of PyObject structs. While strings look mutable in python source code, under the hood new PyObject is created everytime a string is updated.

so "My name is " + my_name + "." usually shows worst performance.
% format
str.format()
''.join(list_of_str_to_join)
these 3 joins the string at once, but with additional parsing.

f-string, introduced at python 3.6, called ''.join() as subroutine in its alpha stage. Then in this discussion, a patch was made to join them straight from the stack using a new Opcode BUILD_STRING.

refrences
https://hg.python.org/cpython/rev/28e280915508#l10.1
https://bugs.python.org/issue27078

profile
software engineer

0개의 댓글