Search code examples
pythonregexcoercion

Python: Identifying a numeric string?


I tried a couple of approaches, I am really only concerned with performance, not correctness. I noticed that the regex based implementation is about 3-4x slower than the one that uses type coercion. Is there another, more efficient way of doing this?

def IsNumber(x):
    try:
        _ = float(x)
    except ValueError:
        return False
    return True

 def IsNumber2(x):
     import re
     if re.match("^\d*.?\d*$", x) == None:
         return False
     return True

Thanks!


Solution

  • First of all, they're not doing the same thing. Floats can be specified as "1e3", for example, and float() will accept that. It's also not coercion, but conversion.

    Secondly, don't import re in IsNumber2, especially if you're trying to use it with timeit. Do the import outside of the function.

    Finally, it doesn't surprise me that float() is faster. It's a dedicated routine written in C for a very specific purpose, while regex must be converted into a form that's interpreted.

    Is your first version, that uses float(), fast enough? It should be, and I don't know of a better way to do the same thing in Python.