PEP 266: Optimizing Global Variable/Attribute Access

Peter Caven p.caven at ieee.org
Wed Aug 15 03:22:41 EDT 2001


Hello Python Optimizers  :-)

After reading PEP 266, I coded the following:

=================
# Tests for speed improvements provided by PEP266
import math

def PEPTest1():
    c = 100000
    while c:
        l = []
        for i in range(10):
            l.append(math.sin(i))
        c -= 1

def PEPTest2():
    c = 100000
    while c:
        l = []
        f = l.append
        m = math.sin
        for i in range(10):
            f(m(i))
        c -= 1

if __name__ == '__main__':
    import profile
    profile.run('PEPTest1()')
    profile.run('PEPTest2()')

================
The profiler reported:

>>>          3 function calls in 4.579 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    4.579    4.579 <string>:1(?)
        1    4.579    4.579    4.579    4.579 PEP266Tests.py:5(PEPTest1)
        1    0.000    0.000    4.579    4.579 profile:0(PEPTest1())
        0    0.000             0.000          profile:0(profiler)


         3 function calls in 3.141 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    3.141    3.141 <string>:1(?)
        1    3.141    3.141    3.141    3.141 PEP266Tests.py:14(PEPTest2)
        1    0.000    0.000    3.141    3.141 profile:0(PEPTest2())
        0    0.000             0.000          profile:0(profiler)


>>> 3.141/4.579
0.68595763267088883
>>>

So it seems that *without* the execution of the proposed TRACK_OBJECT and
UNTRACK_OBJECT there would be about a 31% improvement.
Unless I'm missing something fundamental, if the compiler can be made smart
enough to know where to put TRACK_OBJECT and UNTRACK_OBJECT then why can't
it do the code transformation as above?

Confused,
Peter.







"Skip Montanaro" <skip at pobox.com> wrote in message
news:mailman.997834943.22786.python-list at python.org...
> I wrote:
>
>     For those people on python-list who have no idea what this thread is
>     about, please be patient.  Once Barry Warsaw assigns a PEP number to
it,
>     I will announce it to python-list.
>
> Barry worked his magic, and you can now read the current (first) draft of
> PEP 266 at
>
>     http://python.sourceforge.net/peps/pep-0266.html
>
> Cheers,
>
> Skip
>





More information about the Python-list mailing list