Advertisement
Guest User

Python Benchmark

a guest
Feb 24th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. import re
  2.  
  3. lines =['id INTEGER(11) NOT NULL AUTO_INCREMENT,',
  4. 'id INT(11) NOT NULL AUTO_INCREMENT,',
  5. 'id TINYINT(1) NOT NULL AUTO_INCREMENT,',
  6. 'id DECIMAL(11) NOT NULL AUTO_INCREMENT,',
  7. 'id DOUBLE PRECISION(11) NOT NULL AUTO_INCREMENT,',
  8. 'id DOUBLE(11) NOT NULL AUTO_INCREMENT,',
  9. 'id BOOLEAN(11) NOT NULL AUTO_INCREMENT,',
  10. 'id BIT(11) NOT NULL AUTO_INCREMENT,']
  11.  
  12. lines = [lines[j] + str(i) for i in range(100) for j in range(len(lines))]
  13.  
  14. numeric_types = [
  15.     '(BIG|MEDIUM|SMALL|TINY)*INT(EGER)*(\(.*?\))*',
  16.     'DEC(IMAL)*(\(.*?\))*',
  17.     'NUMERIC(\(.*\))*',
  18.     'FIXED(\(.*\))*',
  19.     'FLOAT(\(.*\))*',
  20.     'DOUBLE( PRECISION)*(\(.*?\))*',
  21.     'REAL(\(.*\))*',
  22.     'BIT',
  23.     'BOOL(EAN)*']
  24.  
  25.  
  26. def method1():
  27.     for line in lines:
  28.         for regex_str in numeric_types:
  29.             if re.search(regex_str, line):
  30.                 line = re.sub(regex_str, "SERIAL", line).replace(
  31.                     " AUTO_INCREMENT", "")
  32.                 break
  33.  
  34. def method2():
  35.     num_type_regex = [re.compile(x) for x in numeric_types]
  36.  
  37.     for line in lines:
  38.         for regex in num_type_regex:
  39.             if regex.search(line):
  40.                 line = regex.sub("SERIAL", line).replace(
  41.                     " AUTO_INCREMENT", "")
  42.                 break
  43. def method3():
  44.     num_type_regex = [re.compile(x) for x in numeric_types]
  45.     regex = re.compile('|'.join(numeric_types))
  46.    
  47.     for line in lines:
  48.         if regex.search(line):
  49.             line = regex.sub("SERIAL", line).replace(
  50.                 " AUTO_INCREMENT", "")
  51.             break
  52.              
  53. from timeit import timeit
  54. count = 3
  55. print('original:', [timeit(method1, number=200) for i in range(count)])
  56. print('compiled:', [timeit(method2, number=200) for i in range(count)])
  57. print('joined:', [timeit(method3, number=200) for i in range(count)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement