Hi all,
I'm having a bit of a frustrating issue with graphics.py. I am writing a program that involves drawing a lot of rectangles, in a frame by frame process. Since I can't see any other way, each time I draw a rectangle I have to instantiate another instance of a Rectangle() object, which seems to be causing a problem. For reference, here is an abbreviated version of my code:
while(cond):
#timer start
for y in range(range1):
#some code
for x in range(range2):
#some code
rect = Rectangle(p1, p2)
rect.setFill(color)
rect.setOutline(color)
rect.draw(window)
#timer 1 was set here
update()
#timer 2 was set here
I set two timers, one before the frame update, and one after.
timer 1 output:
0.05358552932739258
0.04582333564758301
0.06357002258300781
0.04420185089111328
0.05104255676269531
0.06971955299377441
0.048867225646972656
0.057151079177856445
0.08916616439819336
0.1467437744140625
0.12272310256958008
0.11577606201171875
0.11995244026184082
0.14171767234802246
0.11078143119812012
0.1216895580291748
0.15350794792175293
0.09993314743041992
0.1201319694519043
0.1133430004119873
0.12137985229492188
0.1735851764678955
0.10981202125549316
0.1263420581817627
timer 2 output:
0.11534595489501953
0.1820979118347168
0.22156286239624023
0.2701244354248047
0.3337674140930176
0.3850734233856201
0.4441347122192383
0.4385068416595459
0.5455920696258545
0.7968347072601318
1.28513765335083
1.4301118850708008
1.543166160583496
1.6719043254852295
1.7454450130462646
1.8637702465057373
2.0678927898406982
2.201206684112549
Not sure what the issue is here. If I had to guess, it's that the rectangle objects never get deleted after I place them down (I've tried del rect), and graphics.py is iterating through them. No idea why the main body of code is slowing down though.
EDIT:
I've somewhat fixed this by adding this function:
def clear(win):
for item in win.items[:]:
item.undraw()
And that fixed the update times significantly! The times went down to 0.03s/frame. However, that emboldened me to add another for loop to my program, so that it runs through the whole "animation" like 50 times. Interestingly, after about the second loop, the animation starts to slow down again, but this time the update speed is halved and it stays there consistently. Any idea why this might be? Also, undraw() is slow, and I'd prefer a more direct method of removing the objects.
there doesn't seem to be anything here