[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] fast sqrt? and profiling
- To: pygame-users@xxxxxxxx
- Subject: [pygame] fast sqrt? and profiling
- From: Jake b <ninmonkeys@xxxxxxxxx>
- Date: Tue, 20 Jan 2009 23:27:41 -0600
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Wed, 21 Jan 2009 00:27:49 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type; bh=kI+hK2HOxmL2GjDXahkxCnV33UDvtcmDLeTDY9k0x9E=; b=EBwjVZpQ0TkU6GSdJj3LE1pOhxNHEl9b7H2lzewjTTJF4LtsqsSr9VAptStlWL3Dzv EyDGxvY5Radp5KCTraPcwVL1fEYbc3Jf/jIp4QiBPNtrFanH1W3m99rfoYICLUQ7UGRr S9/Nx6dA2EIp+kTj0HXCMcNw5gUc+qKx1Bnqo=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=hdZHvoQm9PxVeGuqlAxildExlAgwzj72j3jtULcKZX59A4fGrIDl1sX0nM0HwJoLoR KhIb+oOi3JBxt7IZNKWASfZmlaJva81f/5ChCxeKMmrKWz6T6ypUwLhbh+4P8hTQtNfH K9szcYDnaDPQanlXXh+aRk+6628kxiCm110h0=
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
I'm writing a prototype, where I want a bunch of 'blobs', that clump/swarm, yet seperate. ( they keep min distance from each other, they don't overlap ( or at least don't stay) )
What I am doing, is every 250ms calculate each units neighbors. ( then, every loop, move away from each cached neighbor. ) My FPS stays around 26-30 until I get 62+ units, then it drops like a stone. [ every Unit() waits 250 for its update, I calc just it, not all Units. ]
From profiling ( and testing ) I know the function to calculate locals ( neighbors ) is causing most of the slowdown.
1) Is there a suggested fast sqrt module thats portable?
2) Is it bad to use isinstance in CPU intensive loops? ( ie: function list_type() )
3) it looks like euclid.Vector2.__sub__ is taking a lot of time. Is this abnormally high? or expected? Meaning should I be using a different lib for speed? [ that particular function uses isinstance() ]
4) Do you have some useful profile queries you've found?
5) Should I be staggering the dist() calls among multiple game updates()? [ sort of am indirectly, but no enforcement. ] Or decouple physics from graphics ?
Heres the functions I mentioned above:
def calc_local(self):
"""re-calc 'who are my neighbors' to cache for later."""
l = [] #first, get list of neighbors
for u in self.game.units.list_type(Unit):
if u == self: continue # don't collide self.
if collide_circle( self.loc, u.loc, self.rad+self.local, u.rad):
l.append(u)
self.local_cache = l
def list_type(self, t):
"""get list of units by class type. example: .list_type(Unit)"""
l = []
for u in self.units:
if isinstance(u, t): l.append(u)
Here's the profiling I'm basing this on:
profile output: ( query = "p.sort_stats('time').print_stats(10)" )
Ordered by: internal time
List reduced from 311 to 10 due to restriction <10>
ncalls tottime percall cumtime percall filename:lineno(function)
388 2.652 0.007 2.652 0.007 {built-in method tick}
1446 1.185 0.001 3.656 0.003 main.py:151(calc_local)
98318 0.790 0.000 1.242 0.000 C:\Python25\lib\euclid.py:172(__sub__)
80355 0.538 0.000 2.071 0.000 C:\jake_root\data\py\jake_includes\jakelib\util.py:49(dist)
194 0.520 0.003 0.520 0.003 {pygame.display.flip}
98583 0.497 0.000 0.681 0.000 C:\Python25\lib\euclid.py:243(__abs__)
80355 0.391 0.000 2.462 0.000 C:\jake_root\data\py\jake_includes\jakelib\util.py:70(collide_circle)
147359 0.348 0.000 0.348 0.000 {isinstance}
147253 0.338 0.000 0.338 0.000 C:\Python25\lib\euclid.py:90(__init__)
12175 0.288 0.000 0.694 0.000 C:\Python25\lib\warnings.py:24(warn)
I know its a lot of questions, that's why I numbered it to simplify replies. Thanks for the help.
--
Jake