loop performance in global namespace (python-2.6.1)

Poor Yorick org.python.pythonlist at pooryorick.com
Thu Mar 12 16:19:47 CET 2009


In the following snippet, the loop in the global namespace takes twice as long
as the loop in the function namespace.  Why?

     limit = 50000000

     def f1():
         counter = 0
         while counter < limit:
             counter += 1
     time1 = time.time()
     f1()
     print(time.time() - time1)
     print('number of locals: ', len(locals()))

     time1 = time.time()
     counter = 0
     while counter < limit:
         counter += 1
     print(time.time() - time1)
     print('number of locals: ', len(locals()))

-- 
Yorick




More information about the Python-list mailing list