Skip to content

Commit

Permalink
Add timings for the render function. RE: #127
Browse files Browse the repository at this point in the history
  • Loading branch information
olls committed Dec 20, 2016
1 parent 2a5360b commit 58d3aa8
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cProfile, pdb, os, sys, glob
import cProfile, pdb, os, sys, glob, timeit

from time import time, sleep
from math import radians
Expand All @@ -15,7 +15,7 @@
def main():
settings = None
try:
meta, settings, profile, debug, name, port = setup()
meta, settings, profile, debug, benchmarks, name, port = setup()

while True:
data = ui.main(meta, settings)
Expand All @@ -33,11 +33,11 @@ def main():
if not server_obj.error:
render_c = import_render_c(settings)
if profile:
cProfile.runctx('game(server_obj, settings, render_c)', globals(), locals(), filename='game.profile')
cProfile.runctx('game(server_obj, settings, render_c, benchmarks)', globals(), locals(), filename='game.profile')
elif debug:
pdb.run('game(server_obj, settings, render_c)', globals(), locals())
pdb.run('game(server_obj, settings, render_c, benchmarks)', globals(), locals())
else:
game(server_obj, settings, render_c)
game(server_obj, settings, render_c, benchmarks)

if server_obj.error:
ui.error(server_obj.error)
Expand All @@ -55,6 +55,18 @@ def setup():

profile = c.getenv_b('PYCRAFT_PROFILE')

benchmarks = c.getenv_b('PYCRAFT_BENCHMARKS')
if benchmarks:
# Monkey patch so timeit returns function result as well as time.
timeit.template = """def inner(_it, _timer{init}):
{setup}
_t0 = _timer()
for _i in _it:
retval = {stmt}
_t1 = _timer()
return _t1 - _t0, retval
"""

# For internal PDB debugging
debug = c.getenv_b('PYCRAFT_DEBUG')

Expand All @@ -69,7 +81,7 @@ def setup():
saves.check_map_dir()

print(HIDE_CUR + CLS)
return meta, settings, profile, debug, name, port
return meta, settings, profile, debug, benchmarks, name, port


def setdown():
Expand All @@ -91,7 +103,7 @@ def import_render_c(settings):
return render_c


def game(server, settings, render_c):
def game(server, settings, render_c, benchmarks):
dt = 0 # Tick
df = 0 # Frame
dc = 0 # Cursor
Expand Down Expand Up @@ -318,8 +330,7 @@ def game(server, settings, render_c):
lights = render.get_lights(extended_view, edges[0], bk_objects)

render_map = render_c.render_map if settings.get('render_c') else render.render_map

out = render_map(
render_args = [
server.map_,
server.slice_heights,
edges,
Expand All @@ -331,7 +342,17 @@ def game(server, settings, render_c):
lights,
settings,
redraw_all
) or ''
]

if benchmarks:
timer = timeit.Timer(lambda: render_map(*render_args))
t, out = timer.timeit(1)
log('Render call time = {}'.format(t), m="benchmarks")
else:
out = render_map(*render_args)

if out is None:
out = ''

redraw_all = False

Expand Down

5 comments on commit 58d3aa8

@geraintwhite
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timeit template doesn't seem to work TypeError: 'float' object is not iterable

@geraintwhite
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python 3.4 uses timeit._template_func, Python 3.5 uses timeit.template.

@geraintwhite
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could just print inside the python function to avoid this problem

@olls
Copy link
Member Author

@olls olls commented on 58d3aa8 Dec 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, we need to test that it doesn't affect the speed too much.

@geraintwhite
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use both then it would work on 3.4 and 3.5 XD

Please sign in to comment.