From 9dc95398e2b2d09be884d71b0dbc6705d7715a2b Mon Sep 17 00:00:00 2001 From: Alexis Roda Date: Sun, 7 Apr 2019 13:09:23 +0200 Subject: Compile regexes lazyly. Profiling shows that compiling the regexes takes most of the execution time. Compiling them when needed instead of compiling all of them upfront decreases execution time from 0.70 to 0.12 seconds. Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 6/1 0.000 0.000 0.702 0.702 {built-in method builtins.exec} 1 0.001 0.001 0.702 0.702 z80count.py:24() 1 0.000 0.000 0.696 0.696 z80count.py:104(main) 1 0.000 0.000 0.692 0.692 z80count.py:65(__init__) 1 0.005 0.005 0.692 0.692 z80count.py:77(_load_table) 1888 0.001 0.000 0.680 0.000 re.py:232(compile) 1891 0.017 0.000 0.680 0.000 re.py:271(_compile) 1631 0.009 0.000 0.647 0.000 sre_compile.py:759(compile) 1631 0.006 0.000 0.325 0.000 sre_parse.py:919(parse) 3263/1631 0.008 0.000 0.312 0.000 sre_parse.py:417(_parse_sub) 1631 0.003 0.000 0.306 0.000 sre_compile.py:598(_code) 3266/1632 0.115 0.000 0.305 0.000 sre_parse.py:475(_parse) 16700/1631 0.112 0.000 0.246 0.000 sre_compile.py:71(_compile) 131243 0.061 0.000 0.086 0.000 sre_parse.py:164(__getitem__) 1631 0.007 0.000 0.057 0.000 sre_compile.py:536(_compile_info) 18331/3262 0.043 0.000 0.053 0.000 sre_parse.py:174(getwidth) 48041 0.018 0.000 0.047 0.000 sre_parse.py:254(get) 9741 0.030 0.000 0.039 0.000 sre_compile.py:276(_optimize_charset) 52307 0.033 0.000 0.033 0.000 sre_parse.py:233(__next) 13432 0.010 0.000 0.029 0.000 sre_compile.py:423(_simple) 173223/156520 0.021 0.000 0.026 0.000 {built-in method builtins.len} 237477 0.024 0.000 0.024 0.000 {method 'append' of 'list' objects} 142999 0.022 0.000 0.022 0.000 {built-in method builtins.isinstance} 9741 0.016 0.000 0.019 0.000 sre_compile.py:249(_compile_charset) 43579 0.014 0.000 0.018 0.000 sre_parse.py:160(__len__) 30678 0.010 0.000 0.014 0.000 sre_parse.py:172(append) 1631 0.001 0.000 0.012 0.000 sre_parse.py:96(closegroup) 1631 0.004 0.000 0.011 0.000 enum.py:827(__and__) 19310 0.009 0.000 0.011 0.000 sre_parse.py:286(tell) 35044 0.011 0.000 0.011 0.000 {built-in method builtins.min} 10767 0.007 0.000 0.010 0.000 sre_parse.py:343(_escape) 23891 0.006 0.000 0.008 0.000 sre_parse.py:249(match) 16701 0.008 0.000 0.008 0.000 sre_parse.py:111(__init__) 3270 0.003 0.000 0.006 0.000 enum.py:281(__call__) --- z80count.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'z80count.py') diff --git a/z80count.py b/z80count.py index 54929dd..a465b3d 100755 --- a/z80count.py +++ b/z80count.py @@ -112,6 +112,8 @@ class Parser(object): if mnemo is None or mnemo not in self._table: return None for entry in self._table[mnemo]: + if "cregex" not in entry: + entry["cregex"] = re.compile(r"^\s*" + entry["regex"] + r"\s*(;.*)?$", re.I) if entry["cregex"].search(line): return entry return None @@ -122,9 +124,6 @@ class Parser(object): with open(table_file, "rt") as fd: table = json.load(fd) - for i in table: - i["cregex"] = re.compile(r"^\s*" + i["regex"] + r"\s*(;.*)?$", re.I) - table.sort(key=lambda o: o["w"]) res = {} for i in table: -- cgit v1.2.3