use sqr((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)), test to see if the exponent
operator is more expensive than a straight multiply. Don't know about
the Palm specifically, but it may help a bit.

Serialization:
if you are only using integers (thus reducing the number of possible
pairs of points), you might get some benefit from serialization: cache
the results from the calculation in a pdb (or array?), (keep the most
common ones) and check it before calculating each time, upcounting each
time you do a lookup. keep the top results while a seek is less than the
time required for the calculation. might want to encode the coordinates
in a UInt32 so the search function only has to do an integer compare
rather than strings.

You could get some performance at the expense of precision by
effectively making your points larger (so that the cache has a match
more often), divide the coordinates of each point so that instead of
160x160 possibilities you have 80x80, or even 40x40 possibilities.

David M

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On 
> Behalf Of Dan Patrutiu
> Sent: Monday, March 17, 2003 4:50 AM
> To: Palm Developer Forum
> Subject: Square root aproximation
> 
> 
>    Hello to all. I know this topic is not a Palm-specific 
> one, but Palm is a place where it applies, because of the 
> known limitations the processor have.
> 
>     So, I need to calculate the distance between two points 
> (x1,y1) and
> (x2,y2) for very much times. That is, calculating the 
> sqrt((x2-x1)^2+(y2-y1)^2). The problem is that, using 
> floating point to do this is very slow and this is time 
> critical for me. I am interested in an approximation which 
> doesn't eat so much cycles and returns a quite close result 
> (I only need the integer square root).
> 
>     Thanks in advance,
>     Dan Patrutiu
> 
> 
> 
> -- 
> For information on using the Palm Developer Forums, or to 
> unsubscribe, please see http://www.palmos.com/dev/support/forums/
> 


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to