Skip to content
This repository has been archived by the owner on Aug 29, 2022. It is now read-only.

Commit

Permalink
further optimisation + debug timers removal
Browse files Browse the repository at this point in the history
  • Loading branch information
RiedleroD committed Sep 4, 2020
1 parent 2dcb83c commit 40b01d6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion CONSTANTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get(self):
else:
return self.t/self.c

profs=[Timer() for i in range(6)]#for speed debugging purposes
profs=[Timer() for i in range(0)]#for speed debugging purposes
colorlamb=lambda perc:[int(SCOLOR[x]*(perc)+ECOLOR[x]*(1-perc)) for x in range(len(SCOLOR))]*2

COLORS=[color for i in range(BUCKLEN) for color in colorlamb(i/BUCKLEN)]
Expand Down
42 changes: 27 additions & 15 deletions Entities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
#!/usr/bin/python3
from CONSTANTS import *
from random import shuffle
import functools,operator
from collections import deque as _deque

class deque(_deque):
def pop(self,index=None):
if index:
x=self[index]
del self[index]
return x
else:
if index==None:
return super().pop()
elif index==0:
return super().popleft()

class Entity:
vl=None
def __init__(self,x,y,w,h,batch,anch=0,bgcolor=(255,255,255)):
Expand Down Expand Up @@ -440,10 +453,10 @@ def __init__(self,x,y,w,h,itemc,batch,anch=0):
else:
self.itemc=itemc
colors=COLORS.copy()
self.items=[i for i in range(abs(itemc))]
self.items=[i for i in range(self.maxic)]
self.racts=set()
self.wacts=set()
self.colors=colors
self.colors=deque(colors)
self.ravl=batch.add(
self.maxic*2,GL_LINES,GRmp,
('v2f/dynamic',[pos for i in range(self.maxic) for pos in self._getract(i)]),
Expand Down Expand Up @@ -483,12 +496,12 @@ def swapitems(self,x,y):
return False
else:
self.items[x],self.items[y]=self.items[y],self.items[x]
self.colors[6*x:6*x+6],self.colors[6*y:6*y+6]=self.colors[6*y:6*y+6],self.colors[6*x:6*x+6]
for j in range(6):
self.colors[6*x+j],self.colors[6*y+j]=self.colors[6*y+j],self.colors[6*x+j]
self.wacts.add(x)
self.wacts.add(y)
return True
def insertitem(self,x,i):
#actually the fastest method I could find, see here: https://stackoverflow.com/a/62608192/10499494
if x>=self.itemc or x<0 or i>self.itemc or i<0:
print(f"out-of-bounds call to INSERT: {x},{i} not in buck[{self.itemc}]")
return False
Expand All @@ -497,21 +510,21 @@ def insertitem(self,x,i):
elif i>x:
self.items[i:i]=self.items[x],
del self.items[x]
self.colors[6*i:6*i]=self.colors[6*x:6*x+6]
del self.colors[6*x:6*x+6]
for j in range(6):#this is magic
self.colors.insert(6*i-1,self.colors.pop(6*x))
self.wacts.add(i-1)
else:
self.items[i:i]=self.items.pop(x),
colors=self.colors[6*x:6*x+6]
del self.colors[6*x:6*x+6]
self.colors[6*i:6*i]=colors
for j in range(6):#also magic
self.colors.insert(6*i+j,self.colors.pop(6*x+j))
self.wacts.add(i)
self.wacts.add(x)
return True
def swap_from(self,x,y,other):
if x>=0 and y>=0 and other.itemc>x and self.itemc>y:
self.items[x],other.items[y]=other.items[y],self.items[x]
self.colors[6*x],other.colors[6*y]=other.colors[6*y],self.colors[6*x]
for j in range(6):
self.colors[6*x+j],other.colors[6*y+j]=other.colors[6*y+j],self.colors[6*x+j]
self.wacts.add(y)
other.wacts.add(x)
return True
Expand All @@ -521,10 +534,9 @@ def swap_from(self,x,y,other):
def insert_from(self,x,y,other):
if other.itemc>=x>=0 and self.itemc>=y>=0:
self.items[y:y]=other.items.pop(x),
self.colors[y*6:y*6]=other.colors[x*6:x*6+6]
del self.colors[-6:]
other.colors.extend((0,0,0,0,0,0))
del other.colors[x*6:x*6+6]
for j in range(6):
self.colors.insert(y*6+j,other.colors.pop(x*6))
other.colors.append(self.colors.pop())
self.itemc+=1
other.itemc-=1
self.wacts.add(y)
Expand Down
16 changes: 2 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,30 +228,18 @@ def on_draw(self):
TIME=t
del t
self.clear()
profs[0].start()
for item in self.labels:#2000µs–5000µs
item.draw()
profs[0].end()
profs[1].start()
for item in self.btns: #100µs
item.draw()
profs[1].end()
profs[2].start()
for item in self.rads: #300µs
item.draw()
self.labels[-1].setText(algs[self.rads[0].getSelected()].desc)
profs[2].end()
profs[3].start()
for item in self.edits: #40µs
item.draw()
profs[3].end()
profs[4].start()
for item in self.bucks: #2000µs–3500µs depending on how much inserting (heavy) vs swapping (light) vs reading (ultra light) is done
item.draw() #5000µs or more when working with multiple buckets (TODO: fix that)
profs[4].end()
profs[5].start()
for item in self.bucks: #1000µs–1500µs depending on how much inserting (heavy) vs swapping (light) vs reading (ultra light) is done and how many buckets are present
item.draw()
self.batch.draw() #1500µs–2000µs
profs[5].end()
pyglet.clock.tick()
def on_mouse_press(self,x,y,button,modifiers):
MP[button]=True
Expand Down

0 comments on commit 40b01d6

Please sign in to comment.