Java 效率 - 点与坐标
使用 Point2D 代替 double x 和 y 值时,效率是否有很大差异?
我正在开发一个程序,该程序有许多圆圈在屏幕上移动。他们每个人都从一个点出发,然后越来越接近目的地(最后,他们停下来)。
使用 .getCurrentLocation().GetY()
等方法(其中 currentLocation
是 Point2D
),我在处理大量数字时遇到性能缓慢的问题的实体。
我不想无缘无故地回去修改我的所有代码,所以我想问的是,仅存储 X 和 Y double
坐标而不是使用 是否会带来显着的性能提升>点
。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您应该使用分析器找出导致性能不佳的原因。我使用 YourKit,但还有其他。
如果
Point2D
确实是问题,您可以将您的点表示为两个double
数组,一个数组代表所有x
坐标一个代表所有y
坐标。这可能比保留 Point2D 对象的集合要快得多。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 twodouble
arrays, one for allx
coordinates and one for ally
coordinates. This is likely to be a lot faster than keeping a collection ofPoint2D
objects.这取决于你是否不断创造新的点。如果没有,即您重用现有的,您不应该获得很大的性能差异。
在大多数情况下,对象的成本不在于存储,而在于对象的创建。
然而,正如 aix 所建议的,尝试使用分析器来查找真正的性能瓶颈。
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.