Advertisement
DigitalMag

try/regex compile banchmark

Jan 29th, 2020
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. import re
  2. import time
  3.  
  4. def reg():
  5.  
  6.     s = raw_input('input:')
  7.     l = 0
  8.  
  9.     t = time.time()
  10.     r = re.compile(r"^\d+$")
  11.  
  12.     for i in range(10000):
  13.  
  14.         if r.match( s):
  15.             l -= 1
  16.         else:
  17.             l += 1
  18.  
  19.     print 'reg:'
  20.     print time.time() - t
  21.  
  22. def try_func():
  23.  
  24.     s = raw_input('input:')
  25.     l = 0
  26.  
  27.     t = time.time()
  28.  
  29.     for i in range(10000):
  30.  
  31.         try:
  32.             d = int(s)
  33.             l -= 1
  34.         except:
  35.             l += 1
  36.  
  37.     print 'try_func:'
  38.     print time.time() - t
  39.  
  40. if __name__ == '__main__':
  41.     reg()
  42.     try_func()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement