Aug-09-2018, 06:14 PM
(Aug-09-2018, 04:54 AM)Skaperen Wrote: how good is the optimization in Python? in this code will it cache the value froms[n]
or will it fetch it twice?
if s[n]<128 or s[n]>191: return -2or should i code this?
x = s[n] if x<128 or x>191: return -2for 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