简体   繁体   English

在性能方面,是一个比java中的自定义类更好还是更差的双精度数组?

[英]In terms of performance, is an array of doubles better or worse than a custom class in java?

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. 我在Java中编写了一个简单的GJK算法实现(凸形碰撞),它涉及到3D矢量的大量简单计算。 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? 在性能与可读性方面,最好将点存储为double [3]并且有一大堆静态方法来处理它们(加,减,点,交叉,否定等)或者使用带有方法的类包含在?

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? 双精度数组的问题在于,如果使用专门的方法,则需要进行简单的减法(例如)多个循环,或者如果它们是硬编码的,则代码变得非常长.Point对象使代码更多更具可读性,但是我认为这不是一个微不足道的性能开销值得吗?

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. 例如,假设您发现应用程序花费10%的时间进行冲突检测,并且50%的时间用于磁盘读取。 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. 如果你可以使用缓存将磁盘读数减少一半,那么你的执行速度提高了25%,这是通过完全消除碰撞检测时间可以达到最佳效果的两倍。

As Donald Knuth said "premature optimization is the root of all evil." 正如唐纳德克努特所说:“过早优化是万恶之源。”

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. 也许你的基类可以是一个数组。

IMHO, the basis tenet of OOP is to have objects defining a contract and fulfilling their responsibility (typically single). 恕我直言,OOP的基本原则是让对象定义合同并履行其职责(通常是单一的)。 So you should definitely go the class way; 所以你绝对应该上课; this will also make your class more maintainable and readable. 这也将使您的课程更易于维护和阅读。

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. 在我理解oops的魔力之前,我也会感觉一样,但现在我真的知道它可以为我的项目创造什么差异。 You can achieve hell lot of propose from oops. 你可以从oops中获得很多建议。

I think 我认为

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

is more like a point than 更像是一个点而不是

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

So, go for class! 所以,去上课吧!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java ConcurrentHashMap 在性能方面优于 HashMap 吗? - Java ConcurrentHashMap is better than HashMap performance wise? 用Doubles Java对数组排序 - Sorting an Array with Doubles Java 初始化一个双精度数组java - Initializing an array of doubles java 为什么haskell表现比java差 - Why is haskell performing worse than java 违反一个空的lambda比检查一个潜在的null lambda更好还是更差? - Is defaulting to an empty lambda better or worse than checking for a potentially null lambda? “带有char的字符串:”+'c'比带有char的字符串更好或更差:“+ c”? - “a string with a char: ” + 'c' better or worse than “a string with a char: ” + “c”? AJAX / JavaScript搜索性能优于Java / Oracle - AJAX/JavaScript search performance better than Java/Oracle Vertx 事件总线的性能与 Java 中的 ConcurrentQueues 一样好还是更好? - Is the performance of Vertx event bus as good or better than ConcurrentQueues in Java? 为什么BufferedReader的性能比BufferedInputStream差得多? - Why is the performance of BufferedReader so much worse than BufferedInputStream? 增强的循环性能比传统的索引查找更差? - Enhanced for loop performance worse than traditional indexed lookup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM