Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- lines =['id INTEGER(11) NOT NULL AUTO_INCREMENT,',
- 'id INT(11) NOT NULL AUTO_INCREMENT,',
- 'id TINYINT(1) NOT NULL AUTO_INCREMENT,',
- 'id DECIMAL(11) NOT NULL AUTO_INCREMENT,',
- 'id DOUBLE PRECISION(11) NOT NULL AUTO_INCREMENT,',
- 'id DOUBLE(11) NOT NULL AUTO_INCREMENT,',
- 'id BOOLEAN(11) NOT NULL AUTO_INCREMENT,',
- 'id BIT(11) NOT NULL AUTO_INCREMENT,']
- lines = [lines[j] + str(i) for i in range(100) for j in range(len(lines))]
- numeric_types = [
- '(BIG|MEDIUM|SMALL|TINY)*INT(EGER)*(\(.*?\))*',
- 'DEC(IMAL)*(\(.*?\))*',
- 'NUMERIC(\(.*\))*',
- 'FIXED(\(.*\))*',
- 'FLOAT(\(.*\))*',
- 'DOUBLE( PRECISION)*(\(.*?\))*',
- 'REAL(\(.*\))*',
- 'BIT',
- 'BOOL(EAN)*']
- def method1():
- for line in lines:
- for regex_str in numeric_types:
- if re.search(regex_str, line):
- line = re.sub(regex_str, "SERIAL", line).replace(
- " AUTO_INCREMENT", "")
- break
- def method2():
- num_type_regex = [re.compile(x) for x in numeric_types]
- for line in lines:
- for regex in num_type_regex:
- if regex.search(line):
- line = regex.sub("SERIAL", line).replace(
- " AUTO_INCREMENT", "")
- break
- def method3():
- num_type_regex = [re.compile(x) for x in numeric_types]
- regex = re.compile('|'.join(numeric_types))
- for line in lines:
- if regex.search(line):
- line = regex.sub("SERIAL", line).replace(
- " AUTO_INCREMENT", "")
- break
- from timeit import timeit
- count = 3
- print('original:', [timeit(method1, number=200) for i in range(count)])
- print('compiled:', [timeit(method2, number=200) for i in range(count)])
- print('joined:', [timeit(method3, number=200) for i in range(count)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement