Hi, Suppose you have a function to validate local/international phone numbers (one space between number groups, no space at start/end, no zero at start) like: def validatePhone(v): if not (9 < len(v) < 19 and '0' < v[0]): *# recall ' ' < '0'* return False for c in v: if '0' <= c <= '9': wasspace = False else: if c != ' ' or wasspace: return False wasspace = True if wasspace: return False # last one return True Just out of curiosity, would JITability of this function change if I put "wasspace = False" just before the loop ? Note: attaching just in case indentation is lost when sending..
2012/1/30 Serhat Sevki Dincer <jfcgauss@gmail.com>:
Hi, Suppose you have a function to validate local/international phone numbers (one space between number groups, no space at start/end, no zero at start) like:
def validatePhone(v): if not (9 < len(v) < 19 and '0' < v[0]): # recall ' ' < '0' return False
for c in v: if '0' <= c <= '9': wasspace = False else: if c != ' ' or wasspace: return False wasspace = True
if wasspace: return False # last one return True
Just out of curiosity, would JITability of this function change if I put "wasspace = False" just before the loop ?
It likely doesn't matter unless you expect phone numbers to be longer than 1000 chars. -- Regards, Benjamin
On Mon, Jan 30, 2012 at 5:56 PM, Benjamin Peterson <benjamin@python.org> wrote:
2012/1/30 Serhat Sevki Dincer <jfcgauss@gmail.com>:
Hi, Suppose you have a function to validate local/international phone numbers (one space between number groups, no space at start/end, no zero at start) like:
def validatePhone(v): if not (9 < len(v) < 19 and '0' < v[0]): # recall ' ' < '0' return False
for c in v: if '0' <= c <= '9': wasspace = False else: if c != ' ' or wasspace: return False wasspace = True
if wasspace: return False # last one return True
Just out of curiosity, would JITability of this function change if I put "wasspace = False" just before the loop ?
It likely doesn't matter unless you expect phone numbers to be longer than 1000 chars.
Or you call the function more than once Benjamin. Answering the *actual* question - I think it does not matter, and for more than one reason in this particular case (on of those being that even though bool objects are boxed, there are only two of them - True and False) Cheers, fijal
On Mon, Jan 30, 2012 at 5:05 PM, Maciej Fijalkowski <fijall@gmail.com>wrote:
I think it does not matter, and for more than one reason in this particular case (on of those being that even though bool objects are boxed, there are only two of them - True and False)
Probably this is another question: what about using 1 and 0 instead of True and False? In (your) jitviewer i see that the "infinite while loops" 'while 1' is better than 'while True'. Am i right? Is it faster in the validatePhone case too? Is it possible to remove this (very small) overhead with the JIT? Thanks -- Davide Setti code: http://github.com/vad
On Mon, Jan 30, 2012 at 7:40 PM, Davide Setti <davide.setti@gmail.com> wrote:
On Mon, Jan 30, 2012 at 5:05 PM, Maciej Fijalkowski <fijall@gmail.com> wrote:
I think it does not matter, and for more than one reason in this particular case (on of those being that even though bool objects are boxed, there are only two of them - True and False)
Probably this is another question: what about using 1 and 0 instead of True and False?\
Not any different in this case.
In (your) jitviewer i see that the "infinite while loops" 'while 1' is better than 'while True'. Am i right? Is it faster in the validatePhone case too? Is it possible to remove this (very small) overhead with the JIT?
Thanks --
Davide Setti code: http://github.com/vad
The actual loop for while 1: and while True: is not different, only the preamble differs. It's generally faster in the interpreted mode because you don't have to check if the global True is not rebinded. Example: while True: ... if some_condition: True = False
participants (4)
-
Benjamin Peterson -
Davide Setti -
Maciej Fijalkowski -
Serhat Sevki Dincer