root@localhost:~# python3.7 -m timeit -s 'x = 6' 'f"{x}"'
5000000 loops, best of 5: 79 nsec per loop
root@localhost:~# python3.7 -m timeit -s 'x = 6' 'str(x)'
1000000 loops, best of 5: 214 nsec per loop
def foo(x):
str(x)
def bar(x):
f"{x}"
2 0 LOAD_GLOBAL 0 (str)
2 LOAD_FAST 0 (x)
4 CALL_FUNCTION 1
6 POP_TOP
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
2 0 LOAD_FAST 0 (f)
2 FORMAT_VALUE 0
4 POP_TOP
6 LOAD_CONST 0 (None)
8 RETURN_VALUE
/* Fast path for common types. */
if (format_spec == NULL || PyUnicode_GET_LENGTH(format_spec) == 0) {
if (PyUnicode_CheckExact(obj)) {
Py_INCREF(obj);
return obj;
}
if (PyLong_CheckExact(obj)) {
return PyObject_Str(obj);
}
}