Why is repr(int) faster than str(int)?

前端 未结 3 620
深忆病人
深忆病人 2021-02-02 08:00

I am wondering why repr(int) is faster than str(int). With the following code snippet:

ROUNDS = 10000

def concat_strings_str():
    re         


        
3条回答
  •  臣服心动
    2021-02-02 08:31

    There are several possibilities because the CPython functions that are responsible for the str and repr return are slightly different.

    But I guess the primary reason is that str is a type (a class) and the str.__new__ method has to call __str__ while repr can directly go to __repr__.

提交回复
热议问题