次の python プログラムは、TkinterCanvas
オブジェクトを作成し、その上にランダム マトリックスを描画します。また、その後の 10 回の更新にかかる時間も測定します。以下の出力からわかるように、この時間はプログラムの進行中に継続的かつ大幅に増加します。この動作の理由は何ですか?どうすれば修正できますか?
from Tkinter import Tk, Canvas
import time
import numpy as np
window = Tk()
nRows = 30
nCols = 30
CELL_SIZE = 10
canvas = Canvas(window, width=CELL_SIZE*nRows,
height=CELL_SIZE*nCols)
canvas.pack()
def drawbox(m):
for y in range(nRows):
for x in range(nCols):
if m[y][x]:
color = '#00FF00'
else:
color = '#000000'
canvas.create_rectangle(CELL_SIZE*x,
CELL_SIZE*y,
CELL_SIZE*x+CELL_SIZE,
CELL_SIZE*y+CELL_SIZE,
fill=color,
outline="#000000", width=1)
count = 0
timeStart = time.time()
while(True):
board = np.random.rand(nRows, nCols) > 0.5
if count % 10 == 0:
print '%.1f seconds'%(time.time() - timeStart)
timeStart = time.time()
count = 0
count += 1
drawbox(board)
canvas.after(5)
canvas.update()
ここに出力があります
0.0 seconds
1.7 seconds
4.1 seconds
6.3 seconds
8.7 seconds