python - Performance of Pypy's islower(), isupper() -


i have program in str.isupper() called in tight loop, , takes around 25% of running time.

it strikes me isupper , islower slower other isx methods, such isalnum, isalpha, isdigit , on. (note: in cpython methods equally slow.)

$ pypy -mtimeit "'a'.islower()"       - 0.0158  usec per loop $ pypy -mtimeit "'a'.isalpha()"       - 0.00251 usec per loop $ pypy -mtimeit "'a'.isupper()"       - 0.0164  usec per loop $ pypy -mtimeit "'a'.isdigit()"       - 0.00236 usec per loop 

i can make faster isupper method myself by

isupper = lambda c: 'a' <= c <= 'z' 

which runs in reasonable 0.00393 usec per loop. of course version doesn't 'work' unicode strings, strings 8bit ascii.


my question if there way 'tell' python skip fancy unicode tests when working simple 'a-z'?

i don't know reason difference, know benchmarks flawed. expression "a".islower() in loop constant-folded away. disappears... might time pass.

we can check "a".isalpha() , pass execute @ same speed: it's speed of empty loop. "a".islower(), i'm not sure understand, it's slower me too, , not --- though it's measuring speed of empty loop, can check (see "jitviewer" if you're interested). mysteries of cpus?

(note has been measured on recent pypy; time ago islower() , isupper() suffered missing optimization, , not constant-folded.)


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -