kanethemediocre / BlingHustle

Open sauce 2d space adventure
GNU General Public License v3.0
0 stars 0 forks source link

Performance optimizations #28

Closed kanethemediocre closed 3 years ago

kanethemediocre commented 3 years ago

In addition to tracking faraway and possibly irrelevant umos (which might turn out to be necessary after all), all distance threshhold calculations are being done based on pythagorean theorem -- distance = sqrt ( a^2 + b^2). That square root is probably slow, and if I just need to check if something is in range, the distance constant could simply be squared so that distance^2 = a^2+b^2.

There may also be benefit to pre-checking x-difference and/or y-difference as a first approximation to avoid the distance calculation altogether in many cases, but I'm not sure that the cost of the extra step is justified by the speed difference.

kanethemediocre commented 3 years ago

Looked into comparative CPU cost of calculations, and square root isn't actually that bad. The study I looked at was compiled C++, which probably changes a lot, but in any case a square root was only about 6X as much time as an add. Trig functions were 15-20x as much as an add, so those are still worth minimizing when possible, but I don't need to worry about using my distance calculation as-is.

Oddly, although multiplication was not much more than an add, division was 4X as much as an addition.