Python Forum
how good is the optimization?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how good is the optimization?
#5
(Aug-09-2018, 04:54 AM)Skaperen Wrote: how good is the optimization in Python? in this code will it cache the value from s[n] or will it fetch it twice?

   if s[n]<128 or s[n]>191:
        return -2
or should i code this?

    x = s[n]
    if x<128 or x>191:
        return -2
for best performance.

I'm not sure this is an optimization issue. s[n] could have a different value every time it's accessed, so it'd HAVE to be accessed twice. This is easy enough to test, even if you don't want to dig into the dis module:
>>> class foo:
...   def __getitem__(self, index):
...     print("indexed!")
...     return index ** 2
...
>>> x = foo()
>>> x[4]
indexed!
16
>>> if x[4] > 10 and x[4] < 50:
...   print("match")
...
indexed!
indexed!
match
Reply


Messages In This Thread
how good is the optimization? - by Skaperen - Aug-09-2018, 04:54 AM
RE: how good is the optimization? - by Gribouillis - Aug-09-2018, 05:52 AM
RE: how good is the optimization? - by DeaD_EyE - Aug-09-2018, 07:16 AM
RE: how good is the optimization? - by Gribouillis - Aug-09-2018, 03:18 PM
RE: how good is the optimization? - by nilamo - Aug-09-2018, 06:14 PM
RE: how good is the optimization? - by micseydel - Aug-09-2018, 08:57 PM
RE: how good is the optimization? - by Skaperen - Aug-10-2018, 12:23 AM
RE: how good is the optimization? - by Gribouillis - Aug-10-2018, 06:00 PM

Forum Jump:

User Panel Messages

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