Browse Source

Improved canvas performances with more built in functions

pull/55/head
Eugenio Parodi 4 years ago
parent
commit
06e2640fef
  1. 39
      TermTk/TTkCore/canvas.py
  2. 84
      tests/timeit/01.doc.comparison.py
  3. 101
      tests/timeit/02.array.copy.py

39
TermTk/TTkCore/canvas.py

@ -70,17 +70,11 @@ class TTkCanvas:
return
baseData = [' ']*w
baseColors = [TTkColor.RST]*w
self._data = [[]]*h
self._colors = [[]]*h
for i in range(0,h):
self._data[i] = baseData.copy()
self._colors[i] = baseColors.copy()
self._data = [baseData.copy() for _ in range(h)]
self._colors = [baseColors.copy() for _ in range(h)]
if self._doubleBuffer:
self._bufferedData = [[]]*h
self._bufferedColors = [[]]*h
for i in range(0,h):
self._bufferedData[i] = baseData.copy()
self._bufferedColors[i] = baseColors.copy()
self._bufferedData = [baseData.copy() for _ in range(h)]
self._bufferedColors = [baseColors.copy() for _ in range(h)]
self._height = h
self._width = w
@ -96,23 +90,18 @@ class TTkCanvas:
self._newWidth = w
self._newHeight = h
def clean(self, pos=(0, 0), size=None):
def clean(self):
if not self._visible: return
x,y = pos
w,h = size or (self._width, self._height)
w,h = self._width, self._height
baseData = [' ']*w
baseColors = [TTkColor.RST]*w
for iy in range(y,y+h):
self._data[iy][x:x+w] = baseData.copy()
self._colors[iy][x:x+w] = baseColors.copy()
self._data = [baseData.copy() for _ in range(h)]
self._colors = [baseColors.copy() for _ in range(h)]
def copy(self):
w,h = self._width, self._height
retData = [[]]*h
retColors = [[]]*h
for iy in range(h):
retData[iy] = self._data[iy].copy()
retColors[iy] = self._colors[iy].copy()
h = self._height
retData = [self._data[i].copy() for i in range(h)]
retColors = [self._colors[i].copy() for i in range(h)]
return retData, retColors
def hide(self):
@ -629,13 +618,11 @@ class TTkCanvas:
def cleanBuffers(self):
if not self._visible: return
x,y = 0,0
w,h = self._width, self._height
baseData = [' ']*w
baseColors = [TTkColor.RST]*w
for iy in range(y,y+h):
self._bufferedData[iy] = baseData.copy()
self._bufferedColors[iy] = baseColors.copy()
self._bufferedData = [baseData.copy() for _ in range(h)]
self._bufferedColors = [baseColors.copy() for _ in range(h)]
def pushToTerminalBuffered(self, x, y, w, h):
# TTkLog.debug("pushToTerminal")

84
tests/timeit/01.doc.comparison.py

@ -0,0 +1,84 @@
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import timeit
import random
words = ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing", "elit,", "sed", "do", "eiusmod", "tempor", "incididunt", "ut", "labore", "et", "dolore", "magna", "aliqua.", "Ut", "enim", "ad", "minim", "veniam,", "quis", "nostrud", "exercitation", "ullamco", "laboris", "nisi", "ut", "aliquip", "ex", "ea", "commodo", "consequat.", "Duis", "aute", "irure", "dolor", "in", "reprehenderit", "in", "voluptate", "velit", "esse", "cillum", "dolore", "eu", "fugiat", "nulla", "pariatur.", "Excepteur", "sint", "occaecat", "cupidatat", "non", "proident,", "sunt", "in", "culpa", "qui", "officia", "deserunt", "mollit", "anim", "id", "est", "laborum."]
def getWords(n):
www = [random.choice(words) for _ in range(n)]
return " ".join(www)
def getSentence(a,b,i):
return " ".join([f"{i} "]+[getWords(random.randint(1,4)) for i in range(0,random.randint(a,b))])
doc1 = [ getSentence(3,10,i) for i in range(10000) ]
doc2 = doc1.copy()
doc2[5000:5050] = [ getSentence(3,10,i) for i in range(50) ]
def test():
for i,l in enumerate(doc1):
if i >= len(doc2) or doc2[i] != l:
return i
return None
def test1():
for i,l in enumerate(reversed(doc1)):
if i >= len(doc2) or doc2[-i-1] != l:
return i
return None
def test2():
for i in range(len(doc1)):
if i >= len(doc2) or doc2[-i-1] != doc1[-i-1]:
return i
return None
def test3():
for i,(a,b) in enumerate(zip(reversed(doc1),reversed(doc2))):
if a!=b:
return f"{i}\n - {a}\n - {b}"
return None
def test4():
for i,(a,b) in enumerate(zip(doc1,doc2)):
if a!=b:
return f"{i}\n - {a}\n - {b}"
return None
print()
loop = 1000
result = timeit.timeit('test()', globals=globals(), number=loop)
print(f"{result / loop:.10f} - {result / loop} {test()}")
result = timeit.timeit('test1()', globals=globals(), number=loop)
print(f"{result / loop:.10f} - {result / loop} {test1()}")
result = timeit.timeit('test2()', globals=globals(), number=loop)
print(f"{result / loop:.10f} - {result / loop} {test2()}")
result = timeit.timeit('test3()', globals=globals(), number=loop)
print(f"{result / loop:.10f} - {result / loop} {test3()}")
result = timeit.timeit('test4()', globals=globals(), number=loop)
print(f"{result / loop:.10f} - {result / loop} {test4()}")

101
tests/timeit/02.array.copy.py

@ -0,0 +1,101 @@
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys, os
import timeit
import random
sys.path.append(os.path.join(sys.path[0],'../..'))
import TermTk as ttk
words = ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing", "elit,", "sed", "do", "eiusmod", "tempor", "incididunt", "ut", "labore", "et", "dolore", "magna", "aliqua.", "Ut", "enim", "ad", "minim", "veniam,", "quis", "nostrud", "exercitation", "ullamco", "laboris", "nisi", "ut", "aliquip", "ex", "ea", "commodo", "consequat.", "Duis", "aute", "irure", "dolor", "in", "reprehenderit", "in", "voluptate", "velit", "esse", "cillum", "dolore", "eu", "fugiat", "nulla", "pariatur.", "Excepteur", "sint", "occaecat", "cupidatat", "non", "proident,", "sunt", "in", "culpa", "qui", "officia", "deserunt", "mollit", "anim", "id", "est", "laborum."]
def getWords(n):
www = [random.choice(words) for _ in range(n)]
return " ".join(www)
def getSentence(a,b,i):
return " ".join([f"{i} "]+[getWords(random.randint(1,4)) for i in range(0,random.randint(a,b))])
doc1 = [ getSentence(3,10,i) for i in range(10000) ]
doc2 = doc1.copy()
doc2[5000:5050] = [ getSentence(3,10,i) for i in range(50) ]
w,h = 200,50
def test():
baseData = [' ']*w
baseColors = [ttk.TTkColor.RST]*w
_data = [[]]*h
_colors = [[]]*h
_data = [[]]*h
_colors = [[]]*h
for i in range(0,h):
_data[i] = baseData.copy()
_colors[i] = baseColors.copy()
return len(_data), len(_colors)
def test1():
baseData = [' ']*w
baseColors = [ttk.TTkColor.RST]*w
_data = [baseData.copy() for _ in range(h)]
_colors = [baseColors.copy() for _ in range(h)]
return len(_data), len(_colors)
def test2():
for i in range(len(doc1)):
if i >= len(doc2) or doc2[-i-1] != doc1[-i-1]:
return i
return None
def test3():
for i,(a,b) in enumerate(zip(reversed(doc1),reversed(doc2))):
if a!=b:
return f"{i}\n - {a}\n - {b}"
return None
def test4():
for i,(a,b) in enumerate(zip(doc1,doc2)):
if a!=b:
return f"{i}\n - {a}\n - {b}"
return None
print()
loop = 1000
result = timeit.timeit('test()', globals=globals(), number=loop)
print(f"{result / loop:.10f} - {result / loop} {test()}")
result = timeit.timeit('test1()', globals=globals(), number=loop)
print(f"{result / loop:.10f} - {result / loop} {test1()}")
result = timeit.timeit('test2()', globals=globals(), number=loop)
print(f"{result / loop:.10f} - {result / loop} {test2()}")
result = timeit.timeit('test3()', globals=globals(), number=loop)
print(f"{result / loop:.10f} - {result / loop} {test3()}")
result = timeit.timeit('test4()', globals=globals(), number=loop)
print(f"{result / loop:.10f} - {result / loop} {test4()}")
Loading…
Cancel
Save