Why does removing the else slow down my code?

前端 未结 3 1388
清歌不尽
清歌不尽 2021-02-02 10:48

Consider the following functions:

def fact1(n):
    if n < 2:
        return 1
    else:
        return n * fact1(n-1)

def fact2(n):
    if n < 2:
                


        
3条回答
  •  太阳男子
    2021-02-02 11:02

    For me, they are virtually the same speed: (Python 2.6.6 on Debian)

    In [4]: %timeit fact1(1)
    10000000 loops, best of 3: 151 ns per loop
    
    In [5]: %timeit fact2(1)
    10000000 loops, best of 3: 154 ns per loop
    

    The byte code is also very similar:

    In [6]: dis.dis(fact1)
      2           0 LOAD_FAST                0 (n)
                  3 LOAD_CONST               1 (2)
                  6 COMPARE_OP               0 (<)
                  9 JUMP_IF_FALSE            5 (to 17)
                 12 POP_TOP             
    
      3          13 LOAD_CONST               2 (1)
                 16 RETURN_VALUE        
            >>   17 POP_TOP             
    
      5          18 LOAD_FAST                0 (n)
                 21 LOAD_GLOBAL              0 (fact)
                 24 LOAD_FAST                0 (n)
                 27 LOAD_CONST               2 (1)
                 30 BINARY_SUBTRACT     
                 31 CALL_FUNCTION            1
                 34 BINARY_MULTIPLY     
                 35 RETURN_VALUE        
                 36 LOAD_CONST               0 (None)
                 39 RETURN_VALUE        
    
    In [7]: dis.dis(fact2)
      2           0 LOAD_FAST                0 (n)
                  3 LOAD_CONST               1 (2)
                  6 COMPARE_OP               0 (<)
                  9 JUMP_IF_FALSE            5 (to 17)
                 12 POP_TOP             
    
      3          13 LOAD_CONST               2 (1)
                 16 RETURN_VALUE        
            >>   17 POP_TOP             
    
      4          18 LOAD_FAST                0 (n)
                 21 LOAD_GLOBAL              0 (fact)
                 24 LOAD_FAST                0 (n)
                 27 LOAD_CONST               2 (1)
                 30 BINARY_SUBTRACT     
                 31 CALL_FUNCTION            1
                 34 BINARY_MULTIPLY     
                 35 RETURN_VALUE        
    

    The only difference is that the version with the else includes code to return None in case control reaches the end of the function body.

提交回复
热议问题