Faster 'if char in string' test

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Thu Jun 24 03:46:49 EDT 2004


Kamilche wrote:
> 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:

Perhaps it would be a good idea to post the snippets at the Python cookbook.

Reinhold

-- 
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich.  Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
  -- David Kastrup in de.comp.os.unix.linux.misc



More information about the Python-list mailing list