Why does Python code run faster in a function?

thedoctar
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 in BASH in Linux.)

real    0m1.841s
user    0m1.828s
sys     0m0.012s

However, if the for loop isn't placed within a function,

for i in xrange(10**8):
    pass

then it runs for a much longer time:

real    0m4.543s
user    0m4.524s
sys     0m0.012s

Why is this?

Katriel

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does this code run faster with a lock?

From Dev

Why does python's built in binary search function run so much faster?

From Dev

Why does python's built in binary search function run so much faster?

From Dev

Why does set( ) make this code run so much faster?

From Dev

Why does CPU run faster than GPU in this code?

From Dev

Why does a function run faster the more I call it?

From Dev

Why does Python run a C++ function faster than C++ running its own function via its main() function?

From Dev

Why does Julia run a function faster than the non-function equivalent?

From Java

Why does C++ code for testing the Collatz conjecture run faster than hand-written assembly?

From Dev

Why does function not run?

From Dev

Why does my function not execute in python code?

From Dev

Why does this code run in javascript ? Function inside an object

From Dev

how to make this code on python run faster?

From Dev

how to make this code on python run faster?

From Dev

Why does clang produce a much faster code than gcc for this simple function involving exponentiation?

From Dev

Why does my python script run much faster when using 5 CPUs rather than 12?

From Dev

Why does Python run my code bottom to top?

From Dev

Why does Python's decorator syntax give faster memoization code than plain wrapper syntax?

From Dev

onclick() Function Does Not Run Code

From Dev

Why does my function not run?

From Dev

Why new Function(code) is faster than directly executing the same code?

From Dev

Why does A* run faster than Dijkstra's algorithm?

From Dev

Why does Excel VBA run significantly faster when Outlook is closed?

From Dev

Why does my logging library cause performance tests to run faster?

From Dev

Why does this program run faster when it's allocated fewer threads?

From Dev

Wrapping code in main function makes it run much faster

From Dev

Why does this code not function correctly?

From Dev

Why does JavaScript code execute faster over time?

From Dev

Why does a strict length function perform noticeably faster?

Related Related

  1. 1

    Why does this code run faster with a lock?

  2. 2

    Why does python's built in binary search function run so much faster?

  3. 3

    Why does python's built in binary search function run so much faster?

  4. 4

    Why does set( ) make this code run so much faster?

  5. 5

    Why does CPU run faster than GPU in this code?

  6. 6

    Why does a function run faster the more I call it?

  7. 7

    Why does Python run a C++ function faster than C++ running its own function via its main() function?

  8. 8

    Why does Julia run a function faster than the non-function equivalent?

  9. 9

    Why does C++ code for testing the Collatz conjecture run faster than hand-written assembly?

  10. 10

    Why does function not run?

  11. 11

    Why does my function not execute in python code?

  12. 12

    Why does this code run in javascript ? Function inside an object

  13. 13

    how to make this code on python run faster?

  14. 14

    how to make this code on python run faster?

  15. 15

    Why does clang produce a much faster code than gcc for this simple function involving exponentiation?

  16. 16

    Why does my python script run much faster when using 5 CPUs rather than 12?

  17. 17

    Why does Python run my code bottom to top?

  18. 18

    Why does Python's decorator syntax give faster memoization code than plain wrapper syntax?

  19. 19

    onclick() Function Does Not Run Code

  20. 20

    Why does my function not run?

  21. 21

    Why new Function(code) is faster than directly executing the same code?

  22. 22

    Why does A* run faster than Dijkstra's algorithm?

  23. 23

    Why does Excel VBA run significantly faster when Outlook is closed?

  24. 24

    Why does my logging library cause performance tests to run faster?

  25. 25

    Why does this program run faster when it's allocated fewer threads?

  26. 26

    Wrapping code in main function makes it run much faster

  27. 27

    Why does this code not function correctly?

  28. 28

    Why does JavaScript code execute faster over time?

  29. 29

    Why does a strict length function perform noticeably faster?

HotTag

Archive