Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster math ops [duplicate]

Possible Duplicate:
Fast algorithm for polar -> cartesian conversion

I am running some pathfinding code (which is a bit slow) through jvisualvm, what I found was that 80% time is being spent in my vector implementation more specifically the part that convert cartesian to polar,

r = Math.sqrt((x * x) + (y * y));
t = Math.atan2(y,x);

are there any old school tricks that would get me some more performance?

like image 476
Hamza Yerlikaya Avatar asked Oct 09 '11 13:10

Hamza Yerlikaya


1 Answers

In my experience in path finding algorithms, the problem is not those lines.

The main questions is "How many times you call these two lines?"

You should investigate your path finding algorithm.

Anyway, if you want reduce the delay of those lines, it is possible to make a pre-calculated table for sqrt and atan2 for each x and y. Or even a table that maps each (x, y) to (r, t) directly.

like image 104
masoud Avatar answered Sep 22 '22 07:09

masoud