Fw: Re: [Tutor] Fastest (x,y) distance calculation

Magnus Lycka magnus at thinkware.se
Thu Sep 11 09:56:10 EDT 2003


Oops, I meant to send this to the entire list...

-----Ursprungligt meddelande-----
Från: Magnus Lycka <magnus at thinkware.se>
Skickat: 2003-09-10  22:35:27
Till: Zak Arntson
Ämne: Re: [Tutor] Fastest (x,y) distance calculation


>     def get_nearest_temple(self, pos):
>         distances = [((pos[0] - tmp.rect.centerx) ** 2 +
>                       (pos[1] - tmp.rect.centery) ** 2,
>                       tmp) for tmp in self.temples]

If self.temples is big, you build a big list here.
Maybe an explicit loop is better?

> I can't think of a better algorithm for getting the nearest object than
> this. Profile tells me this is one of the slower parts of my code. Any
> thoughts/suggestions?

Changing "(a - b) ** 2" to "(a - b) * (a - b)" will probably
save you 30-40%. Multiplication is faster than exponentiation.

Another idea is to consider pyrex or psyco.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se
-------------- next part --------------
An embedded message was scrubbed...
From: Magnus Lycka <magnus at thinkware.se>
Subject: =?ISO-8859-1?B?UmU6IFtUdXRvcl0gRmFzdGVzdCAoeCx5KSBkaXN0YW5jZSBjYWxjdWxhdGlvbg==?=
Date: Wed, 10 Sep 2003 22:35:27 +0200
Size: 1317
Url: http://mail.python.org/pipermail/tutor/attachments/20030911/dc0549ef/attachment-0001.eml


More information about the Tutor mailing list