From 8f249f1d54f50f7e2ed5b9e65aa401fad4f3cb99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 18 Apr 2019 23:37:17 +0200 Subject: [PATCH] improve text.extract_iter() performance by roughly 40% through - inlining code - pre-calculating reused values - entering a try-except block only once --- gallery_dl/text.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/gallery_dl/text.py b/gallery_dl/text.py index b9961053..7e9eeab1 100644 --- a/gallery_dl/text.py +++ b/gallery_dl/text.py @@ -135,12 +135,18 @@ def extract_all(txt, rules, pos=0, values=None): def extract_iter(txt, begin, end, pos=0): - """Yield all values obtained by repeated calls to text.extract""" - while True: - value, pos = extract(txt, begin, end, pos) - if value is None: - return - yield value + """Yield values that would be returned by repeated calls of extract()""" + index = txt.index + lbeg = len(begin) + lend = len(end) + try: + while True: + first = index(begin, pos) + lbeg + last = index(end, first) + pos = last + lend + yield txt[first:last] + except (ValueError, TypeError, AttributeError): + return def parse_bytes(value, default=0, suffixes="bkmgtp"):