isFloat: Without Exception-Handling

Thomas Guettler pan-newsreader at thomas-guettler.de
Tue Sep 24 21:15:30 CEST 2002


On Fri, 20 Sep 2002 16:15:55 +0200, Mark McEahern wrote:

> [Magnus Lycka]
>> Something like this perhaps:
>>
>> import re
>>
>> def isFloat(S):
>>      floatRE = r"^[-+]?(\d+\.?\d*|\.\d+)([eE][-+]?\d+)?$" return
>>      re.match(floatRE, str(S)) is not None
>>
>> I'm not sure it's faster though.
> 
> If you were going to run this lots of times, you might want to compile
> the pattern.  Anyway, the exception based code is faster, by my testing
> (harness at bottom):
> 
>        isFloatExcept 0.220
>            isFloatRE 0.521
>    isFloatRECompiled 0.400

I thought exceptions are slow. But as 
someone mentioned, exception in python
seem to be different from exceptions in 
java or c++.

Thank you for the performance test!

 thomas



More information about the Python-list mailing list