def main():
for i in xrange(10**8):
pass
main()
This piece of code in Python runs inreal 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?
http://stackoverflow.com/questions/11241523/why-does-python-code-run-faster-in-a-function
The difference is that STORE_FAST is faster (!) than STORE_NAME. This is because in a function, i is a local but at toplevel it is a global.