Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
caching attribute reference
#21
@Ofnuts, given the advice at https://wiki.python.org/moin/PythonSpeed...iding_dots... and my inability to find anything saying that CPython does any kind of optimization, I'm really doubtful of that. PyPy very well could.
Reply
#22
Numba?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#23
(Feb-23-2017, 03:53 PM)micseydel Wrote: @Ofnuts, given the advice at https://wiki.python.org/moin/PythonSpeed...iding_dots... and my inability to find anything saying that CPython does any kind of optimization, I'm really doubtful of that. PyPy very well could.

OK, so some experimental evidence that you are right:
import timeit,random

class Counter(object):
    def __init__(self):
        self.someattribute=0
    
    def runAttrib1(self,runs):
        self.someattribute=0
        x=random.randint(1,10)
        for _ in range(runs):
            self.someattribute+=x
        return self.someattribute
            
    def runAttrib2(self,runs):
        self.someattribute=0
        x=random.randint(1,10)
        for _ in range(runs):
            self.someattribute=self.someattribute+x
        return self.someattribute
            
    def runLocal(self,runs):
        self.someattribute=0
        attribcopy=self.someattribute
        x=random.randint(1,10)
        for _ in range(runs):
            attribcopy+=x
        self.someattribute=attribcopy
        return x

counter=Counter()
number=1000
print ("%12s: %12.8f" % ('Attrib1',timeit.timeit(stmt='counter.runAttrib1(100000)',setup='from __main__ import counter', number=number)))
print ("%12s: %12.8f" % ('Attrib2',timeit.timeit(stmt='counter.runAttrib2(100000)',setup='from __main__ import counter', number=number)))
print ("%12s: %12.8f" % ('Local',  timeit.timeit(stmt='counter.runLocal(100000)',  setup='from __main__ import counter', number=number)))
Output for Python2:
Output:
     Attrib1:  10.37185884      Attrib2:   8.38905406        Local:   5.44241190
Output for Python3:
Output:
     Attrib1:   9.38220573      Attrib2:   8.99003031        Local:   4.89760979
So yes, some optimizations aren't done. Strangely var+=x (Attrib1) is slower than var=var+x (Attrib2)...
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#24
it's nice to know
just what is slow
that we may code
without much load
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,513 Sep-07-2020, 08:02 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020