88 Eventually these will be optimized to work better.
99"""
1010import sys
11+ import os
12+
13+ import cProfile as profile
14+ import pstats
1115
1216import itertools
1317import random
@@ -47,6 +51,10 @@ def __init__(self, console):
4751 self .width , self .height = self .console .getSize ()
4852 self .total = self .width * self .height
4953 self .cells = list (itertools .product (range (self .width ), range (self .height )))
54+ self .init ()
55+
56+ def init (self ):
57+ pass
5058
5159 def ev_MOUSEDOWN (self , event ):
5260 self .suspend ()
@@ -64,16 +72,36 @@ def updateTest(self, deltaTime):
6472 char = [random .getrandbits (8 ) for _ in range (self .total )]
6573 fgcolor = (255 , 255 , 255 )
6674 for (x ,y ), bgcolor , char in zip (self .cells , bgcolors , char ):
67- self .console .drawChar (x , y , char , fgcolor , bgcolor )
68-
75+ self .console .draw_char (x , y , char , fgcolor , bgcolor )
76+
77+ class PreCompiledColorTest (TestApp ):
78+ """
79+ This test was to see if the cast to the ctypes Color object was a big
80+ impact on performance, turns out there's no hit with this class.
81+ """
82+
83+ def init (self ):
84+ self .bgcolors = [tdl ._Color (random .getrandbits (7 ),
85+ random .getrandbits (7 ),
86+ random .getrandbits (7 ))
87+ for _ in range (256 )]
88+ self .fgcolor = tdl ._Color (255 , 255 , 255 )
89+
90+ def updateTest (self , deltaTime ):
91+ # getrandbits is around 5x faster than using randint
92+ char = [random .getrandbits (8 ) for _ in range (self .total )]
93+ for (x ,y ), char in zip (self .cells , char ):
94+ self .console .draw_char (x , y , char ,
95+ self .fgcolor , random .choice (self .bgcolors ))
96+
6997
7098class CharOnlyTest (TestApp ):
7199
72100 def updateTest (self , deltaTime ):
73101 self .console .clear ((255 , 255 , 255 ), (0 , 0 , 0 ))
74102 char = [random .getrandbits (8 ) for _ in range (self .total )]
75103 for (x ,y ), char in zip (self .cells , char ):
76- self .console .drawChar (x , y , char , None , None )
104+ self .console .draw_char (x , y , char )
77105
78106class TypewriterCharOnlyTest (TestApp ):
79107
@@ -82,54 +110,64 @@ def updateTest(self, deltaTime):
82110 char = [random .getrandbits (8 ) for _ in range (self .total )]
83111 for (x ,y ), char in zip (self .cells , char ):
84112 self .writer .move (x , y )
85- self .writer .printStr (chr (char ))
113+ self .writer .print_str (chr (char ))
86114
87115class ColorOnlyTest (TestApp ):
88116
89117 def updateTest (self , deltaTime ):
90118 # getrandbits is around 5x faster than using randint
91119 bgcolors = [(random .getrandbits (6 ), random .getrandbits (6 ), random .getrandbits (6 )) for _ in range (self .total )]
92120 for (x ,y ), bgcolor in zip (self .cells , bgcolors ):
93- self .console .drawChar (x , y , None , None , bgcolor )
121+ self .console .draw_char (x , y , None , None , bgcolor )
94122
95123class GetCharTest (TestApp ):
96124
97125 def updateTest (self , deltaTime ):
98126 for (x ,y ) in self .cells :
99- self .console .getChar (x , y )
127+ self .console .get_char (x , y )
100128
101129class SingleRectTest (TestApp ):
102130
103131 def updateTest (self , deltaTime ):
104132 bgcolor = (random .getrandbits (6 ), random .getrandbits (6 ), random .getrandbits (6 ))
105- self .console .drawRect (0 , 0 , None , None , ' ' , (255 , 255 , 255 ), bgcolor )
133+ self .console .draw_rect (0 , 0 , None , None , ' ' , (255 , 255 , 255 ), bgcolor )
106134
107135class DrawStrTest (TestApp ):
108136
109137 def updateTest (self , deltaTime ):
110138 for y in range (self .height ):
111139 bgcolor = (random .getrandbits (6 ), random .getrandbits (6 ), random .getrandbits (6 ))
112140 string = [random .getrandbits (8 ) for x in range (self .width )]
113- self .console .drawStr (0 , y , string , (255 , 255 , 255 ), bgcolor )
141+ self .console .draw_str (0 , y , string , (255 , 255 , 255 ), bgcolor )
114142
115143class BlitScrollTest (TestApp ):
116144 def updateTest (self , deltaTime ):
117145 self .console .scroll (0 , 1 )
118146 for x in range (self .width ):
119147 bgcolor = (random .getrandbits (6 ), random .getrandbits (6 ), random .getrandbits (6 ))
120148 ch = random .getrandbits (8 )
121- self .console .drawChar (x , 0 , ch , bgcolor = bgcolor )
149+ self .console .draw_char (x , 0 , ch , bgcolor = bgcolor )
122150
123151# match libtcod sample screen
124152WIDTH = 46
125153HEIGHT = 20
126154def main ():
127155 console = tdl .init (46 , 20 )
128- for Test in [FullDrawCharTest , CharOnlyTest , TypewriterCharOnlyTest , ColorOnlyTest , GetCharTest ,
156+ for Test in [FullDrawCharTest , PreCompiledColorTest , CharOnlyTest , TypewriterCharOnlyTest , ColorOnlyTest , GetCharTest ,
129157 SingleRectTest , DrawStrTest , BlitScrollTest ]:
130158 Test (console ).run ()
131159 console .clear ()
132160
133161if __name__ == '__main__' :
134- main ()
162+ pr = profile .Profile ()
163+ try :
164+ pr .runcall (main )
165+ finally :
166+ pr .dump_stats ('profile.prof' )
167+ stats = pstats .Stats ('profile.prof' )
168+ stats .strip_dirs ()
169+ stats .sort_stats ('time' )
170+ stats .reverse_order ()
171+ stats .print_stats ()
172+ os .remove ('profile.prof' )
135173
0 commit comments