Python - 打印输出时的差异和+

问题描述 投票:4回答:2

快速而可能是愚蠢的问题。当我需要为输出连接文本和数值时,我通常会使用:

number = 4
print("Two plus two = " + str(number))

但有时我看到:

number = 4
print("Two plus two =",number)

第二个示例不需要类型转换并添加前导空格,但是否则它们执行相同的操作。任何人都知道为什么有两种方法可以做同样的事情?哪种方法“更好”?

python string comma
2个回答
5
投票

重要的是要理解,在print("Two plus two = " + str(number))中,连接操作与print无关,并且在调用print之前发生。

我们来做一些时间:

from timeit import Timer

def with_concat():
    print("Two plus two = " + str(4))

def no_concat():
    print("Two plus two =", 4)

print(min(Timer(with_concat).repeat(100, 100)))
print(min(Timer(no_concat).repeat(100, 100)))

输出

0.0006760049999998685
0.0013034899999999627

反直觉(参见我对问题的评论,字符串连接可能很昂贵)连接的例子实际上是以可重现的方式更快(以2倍!)。但为什么?

让我们检查一下字节码:

from dis import dis

def with_concat():
    print("Two plus two = " + str(4))

def no_concat():
    print("Two plus two =", 4)

dis(with_concat)

输出

 0 LOAD_GLOBAL              0 (print)
 2 LOAD_CONST               1 ('Two plus two = ')
 4 LOAD_GLOBAL              1 (str)
 6 LOAD_CONST               2 (4)
 8 CALL_FUNCTION            1
10 BINARY_ADD
12 CALL_FUNCTION            1
14 POP_TOP
16 LOAD_CONST               0 (None)
18 RETURN_VALUE

dis(no_concat)

输出

 0 LOAD_GLOBAL              0 (print)
 2 LOAD_CONST               1 ('Two plus two =')
 4 LOAD_CONST               2 (4)
 6 CALL_FUNCTION            2
 8 POP_TOP
10 LOAD_CONST               0 (None)
12 RETURN_VALUE

从字节代码来看,它看起来也像no_concat应该更快(更短,更简单的代码)。

延迟必须来自C源代码(至少在CPython的情况下)。

让我们看一下相关的部分:

static PyObject *
builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
{
    .
    .
    .

    for (i = 0; i < PyTuple_Size(args); i++) {
        if (i > 0) {
            if (sep == NULL)
                err = PyFile_WriteString(" ", file);
            else
                err = PyFile_WriteObject(sep, file,
                                         Py_PRINT_RAW);
            if (err)
                return NULL;
        }
        err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
                                 Py_PRINT_RAW);
        if (err)
            return NULL;
    }

    .
    .
    .
}

对我来说,似乎使用print(*args)的开销是由于对PyTuple_GetItem(args, i)的重复调用,并且只有当连接字符串的数量足够大以使连接成为瓶颈时(即比重复调用print(a + lot + of + concatenated + strings))。


0
投票

正如@DeepSpace评论的那样,使用+运算符连接字符串,使用两个args只是使用sep参数作为分隔符(默认为空格)逐个打印字符串。有关详细信息,请查看Python 3.4 PyTuple_GetItem(args, i)

© www.soinside.com 2019 - 2024. All rights reserved.