简体   繁体   English

反复执行getter调用与将值保存在局部变量中

[英]Performance of repeated getter calls vs. saving value in a local variable

Is one of the following options better than the other? 以下选项之一是否比另一个更好? What are the performance considerations? 性能方面的考虑是什么?

class HelloWorld {
    String text = "Hello World";
    public String getText() {
        return this.text;
    }
}

HelloWorld helloWorld = new HelloWorld();

// option A:
for (int i = 0; i < HUGE_NUMBER; i++) System.out.println(helloWorld.getText());

// option B:
String text = helloWorld.getText();
for (int i = 0; i < HUGE_NUMBER; i++) System.out.println(text);

I'm asking specifically about the case where (1) the getter function simply returns a property without performing additional calculations and (2) the property is never changed (there is no need to get a "current" version of it). 我要特别询问的情况是:(1)getter函数仅返回一个属性而无需执行额外的计算,(2)该属性从未更改(无需获取它的“当前”版本)。

The compiler might be smart enough to optimise option A into something similar to option B anyway, so here I assume the compiler does not optimise. 编译器可能足够聪明,无论如何将选项A优化为类似于选项B的东西,因此在此我假定编译器并不乐观。

Option A involves many many calls to getText and many many accesses to the text field. 选项A涉及对getText许多调用以及对text字段的许多访问。 Option B calls getText once and accesses the text variable many many times. 选项B调用一次getText并多次访问text变量。 Therefore, option A will take longer, since calling a method is not instantly. 因此,选项A将花费更长的时间,因为调用方法不是立即的。 The method has to be added to the call stack, and when it returns, be popped from the stack. 该方法必须添加到调用堆栈中,并在返回时从堆栈中弹出。

But does this difference in speed matter ? 但是这种速度差异重要吗? You'd have to check for yourself, using a profiler. 您必须使用分析器进行检查。 When in doubt, use a profiler. 如有疑问,请使用探查器。 If this is actually not causing the performance bottleneck, then changing from option A to B is not gonna help. 如果这实际上不是造成性能瓶颈的原因,那么从选项A更改为B将无济于事。

If you don't even have a performance problem right now, stop worrying about this. 如果您现在甚至没有性能问题,请不要再为此担心。 Wait until you do see your code running slowly, then check with a profiler. 等待直到您看到代码运行缓慢,然后与分析器一起检查。

In your this situation, I am willing to suggest that, you should think over all the angles here, 在您遇到这种情况时,我愿意建议您在这里多加考虑,

Option A - Is directly calling the function getText() while executing System.out.println() , some says this is a bad practice while writing so many lines unless you need to for a simple task. 选项A-在执行System.out.println() getText()时直接调用函数getText() ,有人说System.out.println()这是一种不好的做法,除非您需要执行简单的任务。 some says it halts the line quickly and do some other stuff and then come back here. 有人说它会迅速停止生产线并做其他事情,然后再回到这里。

On the other hand Option B - It creates a new variable in the memory and storing the data which is almost consuming in terms of time and memory, but when you are printing the data it is super fast. 另一方面,选项B-它在内存中创建一个新变量并存储数据,这在时间和内存方面几乎是消耗的,但是当您打印数据时,它是非常快的。

I can't really say which is well performed while people has different preferable scenarios on different level of tasks. 我真的不能说当人们在不同级别的任务上有不同的优选方案时,哪种方法表现良好。

Instead I can help you with some info, which will help you while you go in the future : Some Data Loading Concepts 相反,我可以为您提供一些信息,这些信息将在您将来使用时为您提供帮助: 一些数据加载概念

I hope it helps. 希望对您有所帮助。

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

相关问题 Java:getter方法与公共实例变量:性能和内存 - Java: getter method vs. public instance variable: performance and memory 重复的函数调用与将结果存储在临时变量中 - Repeated function calls vs. storing results in temporary variable 在对象上调用 getter 与将其存储为局部变量(内存占用、性能) - Calling getters on an object vs. storing it as a local variable (memory footprint, performance) Java:实例变量与本地参数 - Java: instance variable vs. local parameters Java编码风格,局部变量vs重复方法调用 - Java coding style, local variables vs repeated method calls 每次从并发 hashmap 读取与将值分配给变量然后使用它的性能 - Performance of reading every time from a concurrent hashmap vs. assigning a value to a variable then using it 虚拟机与本地计算机中的Java App性能 - Java App Performance in Virtual Machine vs. Local Machine 变量引用与重复get调用以避免空指针 - Variable referencing vs. repetitive get calls to avoid null pointers Java局部变量与全局字段-性能 - Java local variable vs global field - Performance Android中的性能或优化:最终局部变量vs局部变量 - performance or optimization in Android : final local variable vs local variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM