简体   繁体   English

Java函数参数与getter的比较,是否有优势?

[英]Java function parameters versus getters, is there an advantage?

Consider the two following situations: 请考虑以下两种情况:

void Foo1(int a, int b, int c, int d) {
    return a + b + c + d;
}

versus

void Foo2(MyArgs args) {
    return args.getA() + args.getB() + args.getC() + args.getD();
}

Is there a speed advantage to either case? 两种情况都有速度优势吗? I have read that the JIT will inline getters. 我读过,JIT将内联吸气剂。 So is there more overhead in passing multiple objects versus a single object to a function, or is this optimized away too? 那么将多个对象传递给一个对象而不是将单个对象传递给一个函数会产生更多的开销,还是将其优化了呢?

I am specifically looking for answers about speed only. 我专门在寻找有关速度的答案。 I am writing some code to recursively search a very large tree, so this type of call will be used many, many times. 我正在编写一些代码来递归搜索非常大的树,因此此类调用将被使用很多次。 My function must return as quickly as possible before a timeout occurs. 在超时之前,我的函数必须尽快返回。 I would like to search as far as possible into the tree. 我想尽可能地搜索树。

If the speed is essentially the same (ie JIT makes both functions essentially equivalent), then I can choose based on readability and maintainability. 如果速度基本相同(即JIT使两个功能基本相同),那么我可以根据可读性和可维护性进行选择。

BTW, is there a magic number of parameters in Java that if you stay below, it is okay but if you go over it is bad? 顺便说一句,有一个神奇的数字在Java的参数,如果你留在下面,它是好的,但如果你去了它是坏? For instance I have worked on machines that if you have 4 or less parameters, then they will be stored in registers, where as more than 4 will get pushed onto the stack? 例如,我在机器上工作过,如果您有4个或更少的参数,那么它们将被存储在寄存器中,在那里有4个以上的参数将被压入堆栈?

BTW, I am still a Java novice... 顺便说一句,我仍然是Java新手...

Also, before you answer "Premature", I understand premature optimization. 另外,在您回答“过早”之前,我了解过早优化。 I am now in the optimization phase. 我现在处于优化阶段。 I have written my recursive function that I am trying to optimize, TIA. 我已经编写了要优化的递归函数TIA。

You seem to be in the design phase, not in the optimization one. 您似乎处于设计阶段,而不是优化阶段。 A method signature is the last thing you would ever check when optimizing. 方法签名是优化时您要检查的最后一件事。

The method signature is decided entirely by what it needs to process. 方法签名完全取决于它需要处理的内容。 If those numbers are related, it does make sense to have a wrapper object for them and find a meaningful name for that wrapper class in your domain. 如果这些数字相关,则有意义的是为其提供一个包装对象,并在您的域中为该包装类找到一个有意义的名称。 The main advantage is that when you'll need to add some extra fields to that object (or remove some), you won't need to touch your method signatures. 主要优点是,当您需要向该对象添加一些额外的字段(或删除一些字段)时,您无需触摸方法签名。 Moreover, you will most likely find other usages for that wrapper object, since it's something that has a meaning in your domain. 此外,您很可能会找到该包装对象的其他用法,因为这在您的域中具有意义。

In terms or performance, there's an overhead of creating a holder object so if those numbers don't naturally belong together, it's a bad idea to define a container for them. 在术语或性能方面,创建一个holder对象的开销很大,因此,如果这些数字不自然地属于在一起,那么为它们定义一个容器是个坏主意。 But if you know they don't belong together, and yet your method needs them all, then you will sort this out from the design phase (by simply listing them individually in the method signature). 但是,如果您知道它们不属于一起,而您的方法需要它们全部,那么您将在设计阶段对其进行梳理(只需在方法签名中单独列出它们)。

Long story short, if you end up modifying method signatures in the "optimization phase", that's a strong signal that you've skipped the design phase. 简而言之,如果您最终在“优化阶段”中修改了方法签名,则表明您已经跳过了设计阶段。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM