Friday, 15 May 2015

Python: Calling functions is really slow? -



Python: Calling functions is really slow? -

i've got element class has functions, 1 this:

def clean(self): self.dirty = false

i have 1024 elements, , i'm calling clean on each 1 of them in while 1: loop.

if stop calling clean method, game framerate goes 76fps 250 fps.

this pretty disturbing. have careful not lag out code?

edit (here's total code):

250 fps code

for layer in self.layers: elements = self.layers[layer] element in elements: if element.getdirty(): element.update() self.renderimage(element.getimage(), element.getrenderpos()) element.clean()

76fps code

for layer in self.layers: elements = self.layers[layer] element in elements: if element.getdirty(): element.update() self.renderimage(element.getimage(), element.getrenderpos()) element.clean()

2edit (here's profiling results):

sat feb 9 22:39:58 2013 stats.dat 23060170 function calls (23049668 primitive calls) in 27.845 seconds ordered by: internal time list reduced 613 20 due restriction <20> ncalls tottime percall cumtime percall filename:lineno(function) 3720076 5.971 0.000 12.048 0.000 element.py:47(clean) 909 4.869 0.005 17.918 0.020 chipengine.py:30(updateelements) 3742947 4.094 0.000 5.443 0.000 copy.py:67(copy) 4101 3.972 0.001 3.972 0.001 engine.py:152(killscheduledelements) 11773 1.321 0.000 1.321 0.000 {method 'blit' of 'pygame.surface' objects} 4 1.210 0.302 1.295 0.324 resourceloader.py:14(__init__) 3720076 0.918 0.000 0.918 0.000 element.py:55(getdirty) 1387 0.712 0.001 0.712 0.001 {built-in method flip} 3742947 0.705 0.000 0.705 0.000 copy.py:102(_copy_immutable) 3728284 0.683 0.000 0.683 0.000 {method 'copy' of 'pygame.rect' objects} 3743140 0.645 0.000 0.645 0.000 {method 'get' of 'dict' objects} 5494 0.566 0.000 0.637 0.000 element.py:89(isbeclouded) 2296 0.291 0.000 0.291 0.000 {built-in method get} 1 0.267 0.267 0.267 0.267 {built-in method init} 1387 0.244 0.000 25.714 0.019 engine.py:67(updateelements) 2295 0.143 0.000 0.143 0.000 {method 'tick' of 'clock' objects} 11764 0.095 0.000 0.169 0.000 element.py:30(update) 8214/17 0.062 0.000 4.455 0.262 engine.py:121(createelement) 40 0.052 0.001 0.052 0.001 {built-in method load_extended} 36656 0.046 0.000 0.067 0.000 element.py:117(iscollidingwith)

the profiling says calling clean method takes 6 out of 28 seconds during profiling. gets called 3.7 1000000 times during time.

that means loop showing must main loop of software. main loop next things:

checks if element dirty. if is, draws it. then cleans it.

since elements not dirty (update() gets called 11 one thousand of these 3.7 1000000 loops), end result main loop doing 1 thing: checking if element dirty, , calling .clean() on it.

by calling clean if element dirty, have cutting main loop in half.

do have careful not lag out code?

yes. if have tight loop time nothing, have create sure loop in fact tight.

this pretty disturbing.

no, it's fundamental computing fact.

python function time call

No comments:

Post a Comment