Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Efficiency - Point vs. Coordinates

Is there a big difference in efficiency when using Point2D instead of double x and y values?

I'm working on a program that has many circles moving around the screen. They each start at one point, and get closer and closer to their destinations (finally, they stop).

Using methods like .getCurrentLocation().GetY() (where currentLocation is a Point2D), I'm experiencing slow performance with any large number of entities.

I don't want to go back and revise all my code for no reason, so I'm asking if I would see a significant performance increase from just storing X and Y double coordinates instead of using Points.

like image 315
Peter Avatar asked Dec 06 '25 08:12

Peter


2 Answers

First of all, you should use a profiler to find out what's causing the poor performance. I use YourKit, but there are others.

If Point2D is indeed the problem, you could represent your points as two double arrays, one for all x coordinates and one for all y coordinates. This is likely to be a lot faster than keeping a collection of Point2D objects.

like image 151
NPE Avatar answered Dec 08 '25 22:12

NPE


That depends on whether you constantly create new points. If not, i.e. you reuse the existing ones, you shouldn't get a big performance difference.

What is costly about objects in most cases is not the storage but the object creation.

However, as suggested by aix, try and use a profiler to find the real performance bottlenecks.

like image 29
Thomas Avatar answered Dec 08 '25 21:12

Thomas