Case-insensitive string comparison

Duncan Booth duncan at NOSPAMrcp.co.uk
Tue Apr 15 05:10:25 EDT 2003


"Avner Ben" <avner at skilldesign.com> wrote in
news:3e9b0e03$1 at news.012.net.il: 

>> >     Does standard Python have case-insensitive string comparison
>> >     and search?
>>
>> No.  You normally accomplish them either with regular expressions
>> (which CAN be compiled into case-insensitive mode) or by coercing
>> strings to one fixed case (e.g. with the .lower method of string
>> objects). 
>>
> 
>     I have tried the lower them all approach, but at the rate of
>     processing 
> I need, it has turned out to reduce application speed by a factor of
> four. What I need is an efficient built-in way of doing this.
> 

That seems very strange to me as lower casing the strings has the
potential to be much more efficient than a case insensitive compare even
when the compare is done by a C routine. Perhaps if you posted a general
outline of your code we could suggest ways to speed it up. The biggest 
potential for code speedup is often to use a different algorithm rather 
than trying to optimise individual operations.

Some ideas to consider:

Avoid lowercasing the same string multiple times. If you are doing more
than one comparison on a string, then lowercasing it once before doing
all the comparisons is better whether you are coding in Python or C. 

Are you using dictionaries enough? If you are looking for matching
strings then a dictionary lookup will beat multiple string comparisons
any day. 

Are you sorting the strings? If so, do your best to avoid using a
comparison function, use a decorated sort. This has two benefits, it
means you only do the lowercasing once, and it removes the Python call
overhead from inside the sort. 

-- 
Duncan Booth                                            
duncan at rcp.co.uk int month(char
*p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3" 
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure? 




More information about the Python-list mailing list