為什麼 Python 程式碼在函數內部執行時速度更快?

DDD
發布: 2024-11-15 13:17:03
原創
420 人瀏覽過

Why is Python Code Faster When Executed Inside a Function?

Why Does Python Code Execute More Swiftly in Functions?

Consider the Python code snippet below, which executes a loop 100 million times.

def main():
    for i in range(10**8):
        pass
main()
登入後複製

When measured in Linux using the time function, this code runs remarkably swiftly:

real    0m1.841s
user    0m1.828s
sys     0m0.012s
登入後複製

Curiously, if the for loop is executed without being enclosed within a function:

for i in range(10**8):
    pass
登入後複製

Its execution time increases considerably:

real    0m4.543s
user    0m4.524s
sys     0m0.012s
登入後複製

Why does this discrepancy occur?

Inside a Function

Examining the bytecode reveals the following when the code is within a function:

  2           0 SETUP_LOOP              20 (to 23)
              3 LOAD_GLOBAL              0 (xrange)
              6 LOAD_CONST               3 (100000000)
              9 CALL_FUNCTION            1
             12 GET_ITER            
        >>   13 FOR_ITER                 6 (to 22)
             16 STORE_FAST               0 (i)

  3          19 JUMP_ABSOLUTE           13
        >>   22 POP_BLOCK           
        >>   23 LOAD_CONST               0 (None)
             26 RETURN_VALUE        
登入後複製

Outside a Function

In contrast, when the code is executed at the top level, the bytecode is as follows:

  1           0 SETUP_LOOP              20 (to 23)
              3 LOAD_NAME                0 (xrange)
              6 LOAD_CONST               3 (100000000)
              9 CALL_FUNCTION            1
             12 GET_ITER            
        >>   13 FOR_ITER                 6 (to 22)
             16 STORE_NAME               1 (i)

  2          19 JUMP_ABSOLUTE           13
        >>   22 POP_BLOCK           
        >>   23 LOAD_CONST               2 (None)
             26 RETURN_VALUE        
登入後複製

The Crux of the Issue

The distinction lies in the use of STORE_FAST versus STORE_NAME. STORE_FAST is employed when the variable (in this case, i) is a local variable (within a function), whereas STORE_NAME is utilized when the variable is a global variable (outside a function). The former is significantly more efficient.

This can be explained by the fact that when a variable is declared as local, the interpreter can optimize the code to use a specific memory location for that variable. However, when a variable is declared as global, the interpreter must search through the entire global scope to find the variable, which is a more time-consuming process.

以上是為什麼 Python 程式碼在函數內部執行時速度更快?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板