[Python-Dev] Draft proposal: Implicit self in Python 3.0

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Sun Jan 8 16:35:53 CET 2006


--- Fredrik Lundh <fredrik at pythonware.com> wrote:
> Ralf W. Grosse-Kunstleve wrote:
>> > ***Please*** make Python more selfish. Note that this is also an obvious
> avenue
> > for significant performance increases. If self is implicit you don't have
> to do
> > the dictionary lookup for "self" all the time as is the case now.
>> what dictionary lookup ?

IIUC, "self" is first looked up in the local dictionary.
Please try the code below to see the performance impact. As an alternative to a
copy/paste exercise try this:
wget http://cci.lbl.gov/~rwgk/python/self_lookup.py
python self_lookup.py
The output with Python 2.4.1/Fedora3/Opteron is:
loop_function: 13.10
 loop_class: 17.11
Cheers,
 Ralf
import time
def loop_function(x, n):
 result = 0
 for i in xrange(n):
 result += x
 return result
class loop_class:
 def __init__(self, x, n):
 self.x = x
 self.n = n
 def __call__(self):
 result = 0
 for i in xrange(self.n):
 result += self.x
 return result
def run(x=3.1415, n=10**8):
 t0 = time.time()
 loop_function(x=x, n=n)
 print "loop_function: %.2f" % (time.time() - t0)
 t0 = time.time()
 loop_class(x=x, n=n)()
 print " loop_class: %.2f" % (time.time() - t0)
if (__name__ == "__main__"):
 run()
		
__________________________________________ 
Yahoo! DSL – Something to write home about. 
Just 16ドル.99/mo. or less. 
dsl.yahoo.com 


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /