Why does Python code run faster in a function?

后端 未结 3 2081
灰色年华
灰色年华 2020-11-22 10:30
def main():
    for i in xrange(10**8):
        pass
main()

This piece of code in Python runs in (Note: The timing is done with the time function

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 10:52

    You might ask why it is faster to store local variables than globals. This is a CPython implementation detail.

    Remember that CPython is compiled to bytecode, which the interpreter runs. When a function is compiled, the local variables are stored in a fixed-size array (not a dict) and variable names are assigned to indexes. This is possible because you can't dynamically add local variables to a function. Then retrieving a local variable is literally a pointer lookup into the list and a refcount increase on the PyObject which is trivial.

    Contrast this to a global lookup (LOAD_GLOBAL), which is a true dict search involving a hash and so on. Incidentally, this is why you need to specify global i if you want it to be global: if you ever assign to a variable inside a scope, the compiler will issue STORE_FASTs for its access unless you tell it not to.

    By the way, global lookups are still pretty optimised. Attribute lookups foo.bar are the really slow ones!

    Here is small illustration on local variable efficiency.

提交回复
热议问题