Python 2 times slower than Perl

Skip Montanaro skip at pobox.com
Tue Jul 17 18:23:39 EDT 2001


    Christopher> This is, in this case, incorrect.  Unless I am horribly
    Christopher> mistaken, the higher cost of dealing with globals is that
    Christopher> you first have to do a failed lookup in the local
    Christopher> dictionary.  (Plus failed lookups are more expensive than
    Christopher> successful ones)

(Note: I trimmed the endpoint from 1e7 to 1e6):

simple1.py:

    i = 2.5
    while i < 1e6:
        j = 2.5 * 2.5
        i += 1
    print i, j

simple2.py:

    def func():
      i = 2.5
      while i < 1e6:
        j = 2.5 * 2.5
        i += 1
      print i, j

    func()

Result:

    % time python simple1.py
    1000000.5 6.25
    4.04user 0.00system 0:04.13elapsed 97%CPU (0avgtext+0avgdata 0maxresident)k
    0inputs+0outputs (323major+138minor)pagefaults 0swaps
    % time python simple2.py
    1000000.5 6.25
    3.00user 0.00system 0:03.06elapsed 97%CPU (0avgtext+0avgdata 0maxresident)k
    0inputs+0outputs (323major+138minor)pagefaults 0swaps

QED.

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list