就性能而言,双精度数组比 java 中的自定义类更好还是更差?

发布于 2024-12-04 14:21:32 字数 246 浏览 0 评论 0原文

我正在用 Java 编写 GJK 算法(凸形状碰撞)的简单实现,它涉及对 3D 向量的大量简单计算。就性能与可读性而言,将点存储为 double[3] 并使用一整套静态方法来处理它们(加、减、点、交叉、求反等)或使用带有这些方法的类会更好吗?包含在?

双精度数组的问题在于,要执行简单的减法(例如),如果使用专门的方法,则需要多个循环,或者如果硬编码,则代码会变得很长。Point 对象会使代码变得很长更具可读性,但是对于我认为不是微不足道的性能开销来说值得吗?

I'm writing a simple implementation of the GJK algorithm (collision of convex shapes) in Java, and it involves a lot of simple calculations on 3D vectors. In terms of performance vs readability, would it be better to store the points as double[3] and have a whole host of static methods to process them (add, subtract, dot, cross, negate etc) or use a class with the methods contained within?

The problem with the array of doubles is that to do a simple subtraction (for instance) multiple loops are required if specialised methods are used, or the code becomes very long if they're hard-coded in. A Point object makes the code much more readable, but is it worth it for what I'm assuming is not an insignificant performance overhead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情丝乱 2024-12-11 14:21:32

我的建议:用你最省力的方法让它发挥作用,然后再优化它。事实可能是,应用程序中的瓶颈完全是其他东西,而碰撞检测方面的改进只是一小部分。

例如,假设您发现应用程序花费 10% 的时间进行冲突检测,50% 的时间进行磁盘读取。如果您可以使用缓存将磁盘读取次数减少一半,则执行速度将提高 25%,这是通过完全消除冲突检测时间所能达到的最佳效果的两倍多。

正如 Donald Knuth 所说“过早的优化是万恶之源”。

My suggestion: make it work using whatever takes the least effort on your part, then optimize it later. It may turn out that the bottleneck in your application is something else entirely, and the improvement in the collision detection is but a minor piece.

For example, say you find that your application spends 10% of its time doing collision detection, and 50% of its time doing disk reads. If you can use caching to cut disk reads in half, you've improved execution by 25%, which is more than twice what you could optimally achieve by eliminating the collision detection time altogether.

As Donald Knuth said "premature optimization is the root of all evil."

吐个泡泡 2024-12-11 14:21:32

如果您需要执行操作、搜索或编辑数据的其他功能,我会使用该对象。也许你的基类可以是一个数组。

I would use the object in case you need to perform other function that manipulate, search or edit the data. Maybe your base class can be an array to.

别靠近我心 2024-12-11 14:21:32

恕我直言,OOP 的基本原则是让对象定义契约并履行其职责(通常是单一的)。所以你绝对应该走班级之路;这也将使您的类更具可维护性和可读性。

IMHO, the basis tenet of OOP is to have objects defining a contract and fulfilling their responsibility (typically single). So you should definitely go the class way; this will also make your class more maintainable and readable.

情独悲 2024-12-11 14:21:32

制作一个类总是更好,这样不仅可以实现可读性,而且还可以实现可扩展性,你可以随时扩展你的类的含义。在我理解 oops 的魔力之前,我也曾经有过同样的感觉,但现在我真正知道它可以给我的项目带来什么差异。你可以从哎呀那里得到很多的求婚。

Making a class is always better, it this way to can not only acieve readability but you will also achieve extensibility, you can extend the meaning of your class anytime. Before I understand the magic of oops i also use to feel the same but now I real know what differences it can create to my project. You can achieve hell lot of propose from oops.

剩一世无双 2024-12-11 14:21:32

我觉得

Point point = new Point(x, y, z);

这更像是一个点

double[] point = {x, y, z};

,所以,去上课吧!

I think

Point point = new Point(x, y, z);

is more like a point than

double[] point = {x, y, z};

So, go for class!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文