forked from libtcod/python-tcod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
117 lines (92 loc) · 3.27 KB
/
benchmark.py
File metadata and controls
117 lines (92 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
from __future__ import print_function
import time
import platform
import tdl
WIDTH = 80 # must be divisible by 16
HEIGHT = 48
RENDERER = 'OpenGL'
log = None
def print_result(string):
print(string)
print(string, file=log)
class Benchmark:
default_frames = 100
def run(self, console, frames=None, times=4):
if times > 1:
print_result('Running %s' % self.__class__.__name__)
while times > 0:
self.run(console, frames, times=1)
times -= 1
print_result('')
return
if frames is None:
frames = self.default_frames
self.total_frames = 0
self.tiles = 0
console.clear()
self.start_time = time.clock()
while self.total_frames < frames:
self.total_frames += 1
self.test(console)
for event in tdl.event.get():
if event.type == 'QUIT':
raise SystemExit('Benchmark Canceled')
self.total_time = time.clock() - self.start_time
self.tiles_per_second = self.tiles / self.total_time
print_result(
'%i tiles drawn in %.2f seconds, %.2f characters/ms, %.2f FPS' %
(self.tiles, self.total_time,self.tiles_per_second / 1000,
self.total_frames / self.total_time))
def test(self, console):
for x,y in console:
console.draw_char(x, y, '.')
tiles += 1
tdl.flush()
class Benchmark_DrawChar_DefaultColor(Benchmark):
def test(self, console):
for x,y in console:
console.draw_char(x, y, 'A')
self.tiles += 1
tdl.flush()
class Benchmark_DrawChar_NoColor(Benchmark):
def test(self, console):
for x,y in console:
console.draw_char(x, y, 'B', None, None)
self.tiles += 1
tdl.flush()
class Benchmark_DrawStr16_DefaultColor(Benchmark):
default_frames = 100
def test(self, console):
for y in range(HEIGHT):
for x in range(0, WIDTH, 16):
console.draw_str(x, y, '0123456789ABCDEF')
self.tiles += 16
tdl.flush()
class Benchmark_DrawStr16_NoColor(Benchmark):
default_frames = 100
def test(self, console):
for y in range(HEIGHT):
for x in range(0, WIDTH, 16):
console.draw_str(x, y, '0123456789ABCDEF', None, None)
self.tiles += 16
tdl.flush()
def run_benchmark():
global log
log = open('results.log', 'a')
print('', file=log)
console = tdl.init(WIDTH, HEIGHT, renderer=RENDERER)
print_result('Benchmark run on %s' % time.ctime())
print_result('Running under %s %s' % (platform.python_implementation(),
platform.python_version()))
print_result('In %s mode' % (['release', 'debug'][__debug__]))
print_result('%i characters/frame' % (WIDTH * HEIGHT))
print_result('Opened console in %s mode' % RENDERER)
Benchmark_DrawChar_DefaultColor().run(console)
Benchmark_DrawChar_NoColor().run(console)
#Benchmark_DrawStr16_DefaultColor().run(console)
#Benchmark_DrawStr16_NoColor().run(console)
log.close()
print('results written to results.log')
if __name__ == '__main__':
run_benchmark()