[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] fast sqrt? and profiling



On Wed, Jan 21, 2009 at 01:43:37PM -0500, Michael George wrote:
> Marius Gedminas wrote:
>> On Wed, Jan 21, 2009 at 07:45:08AM -0800, Ian Mallett wrote:
>>
>>> math.hypot() is good for distance calculation.
>>
>> I did some microbenchmarks a while ago, and math.hypot() was faster than
>> x**2 + y**2.  I was surprised.
>>
> Out of curiousity, did you also try x*x + y*y?

Yes.  Actually, since I was using a vector class, it was self.x *
self.x, and I think the speedup of using math.hypot(self.x, self.y) over
self.x*self.x + self.y*self.y could be explained by reducing by half the
overhead of getattr.

> If it's using general  
> purpose exponentiation routines I could see that being slower than a  
> square root.

Or it could be dynamic dispatch/type checks for using multiple operations
(two multiplications/exponentiations, and one addition) over using a
single primitive implemented in C.

Anyway, it all depends heavily on the actual implementation.  The timeit
module is your friend.  I just wanted to chime in with praise for
math.hypot which is often overlooked.

Marius Gedminas
-- 
Lisp-style macros [...] are to C-style macros what Emacs is to cat.
                -- Jacek Generowicz

Attachment: signature.asc
Description: Digital signature