Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
caching attribute reference
#11
(Feb-22-2017, 01:44 AM)Skaperen Wrote: it might come down to the CPU cost of .attribute vs a plain local variable. given that self is a local variable there is already the cost of accessing self. now add on the cost of accessing an attribute within it ... unless python does any performance optimizing for self specifically (because it is so commonly used) or for repeating expressions.
Could you please show some code that demonstrates what you're doing?
Reply
#12
(Feb-21-2017, 10:51 AM)wavic Wrote: It doesn't matter. Both references are pointers to one memory address

It matters in terms of performance. self.foo has to do an attribute lookup to get that pointer, foo just gives the pointer.

Now, that performance gain is very slight and may not matter in some applications. But if you're doing it a million times, it will start to matter.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#13
true, but what about the steps taken to get there?

1.  lookup 'self' using the locals dictionary
2.  lookup 'foo' using the attributes dictionary of the object found above

vs

1.  lookup 'bar' using the locals dictionary

if i cache self.foo in bar by doing bar = self.foo then i need that value.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#14
(Feb-22-2017, 01:44 AM)Skaperen Wrote: it might come down to the CPU cost of .attribute vs a plain local variable.  given that self is a local variable there is already the cost of accessing self.  now add on the cost of accessing an attribute within it ... unless python does any performance optimizing for self specifically (because it is so commonly used) or for repeating expressions.

It is highly likely that the compiler recognizes the pattern and optimizes around it (assuming it is worth it). Using your own variable may make it less obvious and prevent better optimizations.
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
#15
i'm trying to think of a way to test it that itself won't be optimized.  for example putting some code in a loop could end up with it recognize no change so 10000046 times around won't do so much.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#16
(Feb-22-2017, 09:14 AM)Skaperen Wrote: i'm trying to think of a way to test it that itself won't be optimized.  for example putting some code in a loop could end up with it recognize no change so 10000046 times around won't do so much.

The difficulty is finding something the compiler cannot optimize and that doesn't take time (because you are possibly looking for a handful of processor cycles).

Draw a random integer (to avoid a literal the compiler could optimize). Add it to some variable on each iteration, print the variable at the end (otherwise the compiler may thing it's useless). If you think the compiler could just do a multiplication, then use an array of two ints, and add the number to either member of the array depending on parity.
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
#17
I came across on the web on such thin optimisations wich can impact a significant difference when it comes to iterating over hundreds of thousands or millions of objects.
So, basically Python search first the local scope for functions and other objects then the globals. And assignments like this make sense:

def squared(big_num):
    results = []
    append = list.append

    for n in big_num:
        results.append(n**2)

    return results
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#18
(Feb-22-2017, 08:00 AM)Ofnuts Wrote: It is highly likely that the compiler recognizes the pattern and optimizes around it
Do you mean CPython would do that?
Reply
#19
Would, I don't know. Could, definitely.
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
#20
* Skaperen flips a coin
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,571 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