Pythonic way to determine if one char of many in a string

Peter Otten __peter__ at web.de
Wed Feb 18 15:12:45 EST 2009


Steven D'Aprano wrote:

> On Wed, 18 Feb 2009 07:08:04 +1100, Jervis Whitley wrote:
> 
> 
>>> 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.
> 
> Yes, Python's timing results are often unintuitive.

Indeed.

> It seems to me that I was mistaken -- for large enough input, the running
> time of each version converges to approximately the same speed.

No, you were right. Both any_test() and for_test() use the improvement you 
suggested, i. e. loop over the vowels, not the characters of the word.
Here's the benchmark as it should have been:

$ python -m timeit -s'word = "g"*10000' 'any(v in word for v in "aeiouAEIOU")'
1000 loops, best of 3: 314 usec per loop
$ python -m timeit -s'word = "g"*10000' 'any(c in "aeiouAEIOU" for c in word)'
100 loops, best of 3: 3.48 msec per loop

Of course this shows only the worst case behaviour. The results will vary 
depending on the actual word e. g. "Ug..." or "g...a".

Peter 




More information about the Python-list mailing list