使用for循环比使用reduce更快地求和?

Bry*_*ead 8 python performance

我想看看比使用for循环进行简单的数值运算要快多少.这是我发现的(使用标准timeit库):

In [54]: print(setup)
from operator import add, iadd
r = range(100)

In [55]: print(stmt1)    
c = 0
for i in r:
    c+=i        

In [56]: timeit(stmt1, setup)
Out[56]: 8.948904991149902
In [58]: print(stmt3)    
reduce(add, r)    

In [59]: timeit(stmt3, setup)
Out[59]: 13.316915035247803
Run Code Online (Sandbox Code Playgroud)

再看一点:

In [68]: timeit("1+2", setup)
Out[68]: 0.04145693778991699

In [69]: timeit("add(1,2)", setup)
Out[69]: 0.22807812690734863
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?显然,reduce会比for循环更快,但函数调用似乎占主导地位.减少版本不应该几乎完全在C中运行吗?在for循环版本中使用iadd(c,i)使其在~24秒内运行.为什么使用operator.add比+慢得多?我的印象是+和operator.add运行相同的C代码(我检查以确保operator.add不只是在python中调用+或任何东西).

顺便说一句,只需在~2.3秒内使用总和运行.

In [70]: print(sys.version)
2.7.1 (r271:86882M, Nov 30 2010, 09:39:13) 
[GCC 4.0.1 (Apple Inc. build 5494)]
Run Code Online (Sandbox Code Playgroud)

sam*_*ias 7

reduce(add, r)必须调用add()函数的100倍,因此函数调用的开销加起来-减少使用PyEval_CallObject调用add在每次迭代:

for (;;) {
    ...
    if (result == NULL)
        result = op2;
    else {
        # here it is creating a tuple to pass the previous result and the next
        # value from range(100) into func add():
        PyTuple_SetItem(args, 0, result);
        PyTuple_SetItem(args, 1, op2);
        if ((result = PyEval_CallObject(func, args)) == NULL)
            goto Fail;
    }
Run Code Online (Sandbox Code Playgroud)

更新:回复评论中的问题.

当您键入1 + 2Python源代码时,字节码编译器会执行添加并将该表达式替换为3:

f1 = lambda: 1 + 2
c1 = byteplay.Code.from_code(f1.func_code)
print c1.code

1           1 LOAD_CONST           3
            2 RETURN_VALUE         
Run Code Online (Sandbox Code Playgroud)

如果添加两个变量a + b,编译器将生成字节码,该字节码加载两个变量并执行BINARY_ADD,这比调用函数执行添加要快得多:

f2 = lambda a, b: a + b
c2 = byteplay.Code.from_code(f2.func_code)
print c2.code

1           1 LOAD_FAST            a
            2 LOAD_FAST            b
            3 BINARY_ADD           
            4 RETURN_VALUE         
Run Code Online (Sandbox Code Playgroud)