Skip to content

Commit b3fc19f

Browse files
4B796C65@gmail.com4B796C65@gmail.com
authored andcommitted
minor stress_test.py update
1 parent b10573b commit b3fc19f

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

examples/stress_test.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,65 @@
77
Eventually these will be optimized to work better.
88
"""
99
import sys
10-
sys.path.insert(0, '../')
1110

11+
import itertools
1212
import random
13-
import cProfile
13+
import time
1414

15+
sys.path.insert(0, '../')
1516
import tdl
1617

18+
class StopWatch:
19+
"Simple tool used to count time within a block using the with statement"
20+
MAXSNAPSHOTS = 10
21+
22+
def __init__(self):
23+
self.enterTime = None
24+
self.snapshots = []
25+
26+
def __enter__(self):
27+
self.enterTime = time.clock()
28+
29+
def __exit__(self, *exc):
30+
self.snapshots.append(time.clock() - self.enterTime)
31+
if len(self.snapshots) > self.MAXSNAPSHOTS:
32+
self.snapshots.pop(0)
33+
34+
def getMeanTime(self):
35+
if not self.snapshots:
36+
return 0
37+
return sum(self.snapshots) / len(self.snapshots)
38+
39+
1740
def main():
1841

1942
WIDTH = 80
2043
HEIGHT = 60
21-
44+
TOTAL = WIDTH * HEIGHT
45+
# a list containing all x,y pairs
46+
CELLS = list(itertools.product(range(WIDTH), range(HEIGHT)))
47+
2248
console = tdl.init(WIDTH, HEIGHT)
23-
49+
fullTimer = StopWatch()
50+
randomTimer = StopWatch()
51+
drawTimer = StopWatch()
52+
flushTimer = StopWatch()
2453
while 1:
2554
for event in tdl.event.get():
2655
if event.type == tdl.QUIT:
2756
raise SystemExit()
28-
for y in range(HEIGHT):
29-
bgcolor = (random.randint(0, 64), random.randint(0, 64), random.randint(0, 64))
30-
for x in range(WIDTH):
31-
console.drawChar(random.randint(0, 255), fgcolor=(255, 255, 255), bgcolor=bgcolor)
32-
tdl.flush()
57+
with randomTimer:
58+
# getrandbits is around 5x faster than using randint
59+
bgcolors = [(random.getrandbits(6), random.getrandbits(6), random.getrandbits(6)) for _ in range(TOTAL)]
60+
char = [random.getrandbits(8) for _ in range(TOTAL)]
61+
with drawTimer:
62+
for (x,y), bgcolor, char in zip(CELLS, bgcolors, char):
63+
console.drawChar(char=char, x=x, y=y, fgcolor=(255, 255, 255), bgcolor=bgcolor)
64+
console.drawStr('Random%7.2fms ' % (randomTimer.getMeanTime() * 1000), 0, 0, tdl.C_WHITE, tdl.C_BLACK)
65+
console.drawStr('DrawCh%7.2fms ' % (drawTimer.getMeanTime() * 1000), 0, 1, tdl.C_WHITE, tdl.C_BLACK)
66+
console.drawStr('Flush %7.2fms ' % (flushTimer.getMeanTime() * 1000), 0, 2, tdl.C_WHITE, tdl.C_BLACK)
67+
with flushTimer:
68+
tdl.flush()
3369
tdl.setTitle('%i FPS' % tdl.getFPS())
3470

3571
if __name__ == '__main__':

0 commit comments

Comments
 (0)