# FastMath

Robo Home | Changes | Preferences | AllPages

In the interest of reducing the number of SlowBots out there, does anyone have algorithms to approximate square roots, sines, etc.? Ideally these would be continuous functions rather than simply using the nearest cached value. For that matter, are approximations really faster? I got worried when I saw that the default implementation of Math.sqrt() is to call StrictMath?.sqrt() but it might be fast enough already :-p

/SquareRoot /Sine? /Cosine? /Tangent? /AngleFromX1Y1toX2Y2?

Edit Conflict: "Are you sure an approx algo will be much faster than the built in ones? I think it's best to avoid them completly if you can, for example it's often possible to leave things as squares, hence the Point2D.DistanceSq?() method, and similar. -- Tango"

Seems we had the same thought... -- Tango

Next question is if fast as ever math functions will help those SlowBots. -- PEZ

It would if they have a slow function in an inner loop and we can find a significantly faster one. :-p --David Alves

most of my PM guns use squareroots in the inner loop to compare the positions. I also thought about to remove this and just use the square of the distance. The bad thing is I couldn't recognize a difference at all. -- rozu

Doesn't surprise me. I think it's the loops in themselves that go for too long and too deep. I have no idea if it must be so though since I have no clue what's going on. =) -- PEZ

The normal way to make trig functions work fast is to load the values into an Array at start-time and use values from that. Gives slightly less precision but it ought to be much faster (though Java arrays are kind of slow. :-\). -- Jamougha

Is it any faster to multiply a number with the * operator in java by a power of 10, a power of 2, or does it not matter (i'm talking about double * number, so the bit shift doesnt apply)? -- Scoob

Is anyone using a performance profiler with Eclipse effectively with their robots? I am a little unclear how it would even work, since you usually use such a tool by running your code as an application / unit test. -- The Martinator

yep ...

• create a robocode project
• make your robot project depend on the robocode project
• compile your robot, package it, move it to the robocode robots directory (you can automate this with a custom ant build)
• set up a runpoint that runs robocode
• now you're ready to debug your code, use JProfiler, or the free profiler plugin.
-- FnH

Robo Home | Changes | Preferences | AllPages