Feb-23-2017, 03:53 PM
@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.
caching attribute reference
|
Feb-23-2017, 03:53 PM
@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.
Feb-23-2017, 04:20 PM
(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 for Python3: 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
Feb-24-2017, 06:04 AM
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. |
|
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 |