简体   繁体   English

Java优化:局部变量或函数调用

[英]Java optimization : local variable or function call

Which would you do ? 你会做什么?

 doThings(folder.getInstructions()) ;
 for (Instruction instruction : folder.getInstructions()) {
    // do things
 }
functionCall(folder.getInstructions()) ;

Or this : 或这个 :

instructions = folder.getInstructions() ;
doThings(instructions)
for (Instruction instruction : instructions) {
  // do things
}
functionCall(instructions) ;

Above all, I would like to know when it is more efficient to store a value in a local variable, and when it is better to make function calls. 最重要的是,我想知道何时将值存储在局部变量中更有效,以及什么时候更好地进行函数调用。

More readable is more efficient. 更具可读性更有效。 Temporary expressions and local variables need the same space and from CPU/JVM perspective it doesn't make much difference. 临时表达式和局部变量需要相同的空间,从CPU / JVM的角度来看,它没有太大的区别。 JVM will do a better job optimizing/inling it. JVM将更好地优化/使用它。

However if getInstructions() method call is expensive, cache it in local variable. 但是,如果getInstructions()方法调用很昂贵,请将其缓存在局部变量中。 If it's just a plain getter, it will be inlined anyway. 如果它只是一个简单的吸气剂,它无论如何都会被内联。 Also IMHO in your particular case local variable is more readable and maybe even more correct if getInstructions() may have different results over time. 此外恕我直言,你的具体情况局部变量更具可读性,甚至更正确的,如果getInstructions()可能有一段时间不同的结果。

That entirely depends on what getInstructions() does. 这完全取决于getInstructions()作用。 If it's just returning the value of a field - and if you're confident that the field value won't change between calls - then you probably won't see any efficiency differences between the two snippets. 如果它只是返回字段的值 - 并且如果您确信字段值不会在调用之间发生变化 - 那么您可能看不到两个片段之间的任何效率差异。

If, on the other hand, getInstructions() needs to make a dozen web requests, then clearly you want to avoid calling that several times. 另一方面,如果getInstructions()需要发出十几个Web请求,那么显然你想避免多次调用它。

Readability is more important than efficiency though. 可读性比效率更重要。 In this case I find the second option more readable anyway - it's clearer that you want to take three separate steps (two method calls and a loop) with the same value. 在这种情况下,我发现第二个选项无论如何都更具可读性 - 您希望采用具有相同值的三个单独步骤(两个方法调用和一个循环)更清楚。 On the other hand, I'm quite happy to write something like: 另一方面,我很高兴写下这样的东西:

for (int i = 0; i < text.length(); i++) {
    ...
}

rather than breaking that out into a separate variable: 而不是将分解为单独的变量:

int length;
for (int i = 0; i < length; i++) {
    ...
}

It really depends on the context. 这取决于具体情况。 Sometimes an extra variable helps, sometimes it doesn't, from a readability perspective. 有时,从可读性的角度来看,额外的变量有助于(有时不会)。 The efficiency perspective depends completely on what the method call is doing, and whether it's "inlinable" for the JIT. 效率视角完全取决于方法调用正在做什么,以及它是否对JIT来说是“无法解决的”。

Whenever, you want to use the result of a method more than once, its better to store the result of method invocation in some temp variable.. This saves processing time of method invocation.. This will not affect as much in this case.. But may affect when there are many invocation.. 每当你想要多次使用方法的结果时,最好将方法调用的结果存储在某个临时变量中。这样可以节省方法调用的处理时间。在这种情况下,这不会影响太多。但是当有很多调用时可能会影响..

Also, remember, your local variables are stored on stack . 另外,请记住,您的局部变量存储在stack So, having a temporary local variable is occupying space on stack. 因此,拥有临时局部变量会占用堆栈空间。 Although this is not much of a concern in small cases like this one.. But unneccessary local variables, should be avoided.. 虽然这在像这样的小案例中并不是很重要..但是应该避免不必要的局部变量。

So, there are pros and cons of both methods.. 因此,有两种方法的优点缺点 ..

My answer would be dependent on what type of application that I am trying to develop. 我的答案取决于我正在尝试开发的应用程序类型 For example, 例如,

The first code block is better if the values are changes rapidly during the application. 如果值在应用期间快速变化,则第一个代码块会更好。 This is good if you need accurate results. 如果您需要准确的结果,这很好。

The second one is good if you are sure that the values initially fetched would not affect the other parts of the code. 如果您确定最初提取的值不会影响代码的其他部分,则第二个是好的。

I believe it depends upon the situation. 我认为这取决于具体情况。 if it requires to make the same method call many times(and you are getting the same value) and better make one call and store it into some local variable. 如果它需要多次调用相同的方法(并且您获得相同的值)并且更好地进行一次调用并将其存储到某个局部变量中。

and if you need to call the function only once. 如果你只需要调用一次该函数。 no need to go for local variables. 无需去寻找局部变量。

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

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