Advertisement
Guest User

whoisfaster.py

a guest
Dec 23rd, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. import re
  2. import timeit
  3.  
  4.  
  5.  
  6. def is_int_trying(string:str):
  7.     try:
  8.         return float(string).is_integer()
  9.     except ValueError:
  10.         return False
  11.  
  12.  
  13. pattern = re.compile(r"([\+\-]?\d+[.[0]]?)")
  14. def is_int_re(string:str):
  15.     return re.match(pattern, string)
  16.  
  17. cases = {
  18.     "0": True, "-0": True, "+0": True,
  19.     "1": True, "-1": True, "+1": True,
  20.     "0.1": False, "-0.1": False, "+0.1": False,
  21.     "1.1": False, "-1.1": False, "+1.1": False,
  22.     "0.000000000000000000001": False,
  23.     "-0.000000000000000000001": False,
  24.     "+0.000000000000000000001": False,
  25.     "⁰12345": False, "-⁰12345": False, "+⁰12345": False
  26. }
  27.  
  28. for case, isint in cases.items():
  29.     for checker in (is_int_trying, is_int_re):
  30.         print("func: {}, val: {}\n  correct: {}\n  time: ".format(
  31.             checker.__name__, case, checker(case)==isint
  32.         ), end="")
  33.         print(timeit.timeit(
  34.             "checker(case)==isint",
  35.             globals=globals(),
  36.             number=100000
  37.         ))
  38.     print()
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement