Faster 'if char in string' test

Pierre-Frédéric Caillaud peufeu at free.fr
Thu Jun 24 09:55:42 CEST 2004



	why don't you use translate to delete all the valid characters and test  
if the resulting string is not not empty ? this will save you two calls to  
len()

	hehe...




> I was looking for a way to speed up detecting invalid characters in my
> TCP string, and thought of yet another use for the translate function!
> If you were to 'translate out' the bad characters, and compare string
> lengths afterwards, you would know whether or not the line contained
> invalid characters. The new method is more than 10x faster than the
> standard 'if char in string' test! So - here's the code plus sample
> timings:
>





-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/



More information about the Python-list mailing list