Pythonic way to determine if one char of many in a string
Jervis Whitley
jervisau at gmail.com
Tue Feb 17 15:08:04 EST 2009
>
> This moves the for-loop out of slow Python into fast C and should be much,
> much faster for very large input.
>
_Should_ be faster.
Here is my test on an XP system Python 2.5.4. I had similar results on
python 2.7 trunk.
WORD = 'g' * 100
WORD2 = 'g' * 50 + 'U'
BIGWORD = 'g' * 10000 + 'U'
def any_test(word):
return any(vowel in word for vowel in 'aeiouAEIOU')
def for_test(word):
for vowel in 'aeiouAEIOU':
if vowel in word:
return True
else:
return False
**no vowels**
any: [0.36063678618957751, 0.36116506191682773, 0.36212355395824081]
for: [0.24044885376801672, 0.2417684017413404, 0.24084797257163482]
**vowel 'U' final char**
any: [0.38218764069443112, 0.38431925474244588, 0.38238668882188831]
for: [0.16398578356553717, 0.16433223810347286, 0.16593555537176385]
**BIG word vowel 'U' final char**
any: [8.0007259193539895, 7.9797344140269644, 7.8901742633514012]
for: [7.6664422372764101, 7.6784683633957584, 7.6683055766498001]
Cheers,
More information about the Python-list
mailing list