diff --git a/TermTk/TTkCore/string.py b/TermTk/TTkCore/string.py index 1ced7ce9..a50c7f66 100644 --- a/TermTk/TTkCore/string.py +++ b/TermTk/TTkCore/string.py @@ -589,10 +589,12 @@ class TTkString(): a is None and b is None ) else self._termWidthW() def _checkWidth(self): - self._hasSpecialWidth = self._termWidthW() if ( - any(ord(ch)>=0x300 for ch in self._text) and ( - any(unicodedata.east_asian_width(ch) == 'W' for ch in self._text) or - any(unicodedata.category(ch) in ('Me','Mn') for ch in self._text) ) ) else None + # from: tests/timeit/09.widechar.check.py + # the first not halfsize char is 0x300 + # this check is ~3 times faster than the 2 combined unicode checks + # and will quickly filter out the (more common) simple ascii text + tw = self._termWidthW() if any(ord(ch)>=0x300 for ch in self._text) else None + self._hasSpecialWidth = tw if tw != len(self._text) else None def _termWidthW(self): ''' String displayed length diff --git a/tests/timeit/09.widechar.check.py b/tests/timeit/09.widechar.check.py index d5b3b7db..d9346ded 100644 --- a/tests/timeit/09.widechar.check.py +++ b/tests/timeit/09.widechar.check.py @@ -40,7 +40,7 @@ print(f"Create CharSetStringTest...") cstr = "" cstrw = "" for _ in range(0x4000): - cstr += chr(random.randint(0x100,0x300)) + cstr += chr(random.randint(0x100,0x2FF)) cstrw += chr(random.randint(0x100,0x20000)) print(f"Create CharSetStringTest DONE!!!") @@ -50,42 +50,40 @@ print(f"len cstrw 0x{len(cstrw):04x}") # print([f"'{ch}':{unicodedata.east_asian_width(ch)}:{unicodedata.category(ch)}" for ch in cstr]) # print([f"'{ch}':{unicodedata.east_asian_width(ch)}:{unicodedata.category(ch)}" for ch in cstrw]) -a=10 -def test1(x): - text = cstr +def _tw(text): return ( len(text) + - sum(unicodedata.east_asian_width(ch) == 'W' for ch in text) - - sum(unicodedata.category(ch) in ('Me','Mn') for ch in text) ) + sum(unicodedata.east_asian_width(ch) == 'W' for ch in text) - + sum(unicodedata.category(ch) in ('Me','Mn') for ch in text) ) -def test2(x): - text = cstrw +def test1(text): return ( len(text) + sum(unicodedata.east_asian_width(ch) == 'W' for ch in text) - sum(unicodedata.category(ch) in ('Me','Mn') for ch in text) ) -def test3(x): - text = cstr - return ( any(unicodedata.east_asian_width(ch) == 'W' for ch in text) or - any(unicodedata.category(ch) in ('Me','Mn') for ch in text) ) +def test2(text): + tw = _tw(text) if any(ord(ch)>=0x300 for ch in text) else None + return tw if tw != len(text) else None -def test4(x): - text = cstrw +def test3(text): return ( any(unicodedata.east_asian_width(ch) == 'W' for ch in text) or any(unicodedata.category(ch) in ('Me','Mn') for ch in text) ) -def test5(x): - text = cstr - return any(ord(ch)>=0x300 for ch in text) +def test4(text): + return (any(ord(ch)>=0x300 for ch in text) and + ( any(unicodedata.east_asian_width(ch) == 'W' for ch in text) or + any(unicodedata.category(ch) in ('Me','Mn') for ch in text) ) ) -def test6(x): - text = cstrw +def test5(text): return any(ord(ch)>=0x300 for ch in text) +def test6(text): + return any(ord(ch)>=0x300 for ch in text) +loop = 200 -loop = 100 +a=cstr result = timeit.timeit('test1(a)', globals=globals(), number=loop) print(f"1a {result / loop:.10f} - {result / loop} {test1(a)}") result = timeit.timeit('test2(a)', globals=globals(), number=loop) @@ -98,3 +96,17 @@ result = timeit.timeit('test5(a)', globals=globals(), number=loop) print(f"5a {result / loop:.10f} - {result / loop} {test5(a)}") result = timeit.timeit('test6(a)', globals=globals(), number=loop) print(f"6a {result / loop:.10f} - {result / loop} {test6(a)}") + +a=cstrw +result = timeit.timeit('test1(a)', globals=globals(), number=loop) +print(f"1b {result / loop:.10f} - {result / loop} {test1(a)}") +result = timeit.timeit('test2(a)', globals=globals(), number=loop) +print(f"2b {result / loop:.10f} - {result / loop} {test2(a)}") +result = timeit.timeit('test3(a)', globals=globals(), number=loop) +print(f"3b {result / loop:.10f} - {result / loop} {test3(a)}") +result = timeit.timeit('test4(a)', globals=globals(), number=loop) +print(f"4b {result / loop:.10f} - {result / loop} {test4(a)}") +result = timeit.timeit('test5(a)', globals=globals(), number=loop) +print(f"5b {result / loop:.10f} - {result / loop} {test5(a)}") +result = timeit.timeit('test6(a)', globals=globals(), number=loop) +print(f"6b {result / loop:.10f} - {result / loop} {test6(a)}") \ No newline at end of file