在对象上调用getter而不是将其存储为局部变量(内存占用,性能) [英] Calling getters on an object vs. storing it as a local variable (memory footprint, performance)

查看:151
本文介绍了在对象上调用getter而不是将其存储为局部变量(内存占用,性能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我们拨打电话 listType.getDescription()两次:

In the following piece of code we make a call listType.getDescription() twice:

for (ListType listType: this.listTypeManager.getSelectableListTypes())
{
    if (listType.getDescription() != null)
    {
        children.add(new SelectItem( listType.getId() , listType.getDescription()));
    }
}

我倾向于重构代码以使用单个变量:

I would tend to refactor the code to use a single variable:

for (ListType listType: this.listTypeManager.getSelectableListTypes())
{
    String description = listType.getDescription();

    if (description != null)
    {
        children.add(new SelectItem(listType.getId() ,description));
    }
}

我的理解是JVM以某种方式针对原始进行了优化代码,尤其是嵌套调用,如 children.add(new SelectItem(listType.getId(),listType.getDescription()));

My understanding is the JVM is somehow optimized for the original code and especially nesting calls like children.add(new SelectItem(listType.getId(), listType.getDescription()));.

比较两个选项,哪一个是首选方法?为什么?这就是内存占用,性能,可读性/易用性以及其他现在还没有想到的其他内容。

Comparing the two options, which one is the preferred method and why? That is in terms of memory footprint, performance, readability/ease, and others that don't come to my mind right now.

后一段代码片段何时变得更多优于前者,也就是说,使用临时局部变量时,任何(近似)数量的 listType.getDescription()调用变得更加可取,因为 listType.getDescription()总是需要一些堆栈操作来存储这个对象?

When does the latter code snippet become more advantageous over the former, that is, is there any (approximate) number of listType.getDescription() calls when using a temp local variable becomes more desirable, as listType.getDescription() always requires some stack operations to store the this object?

推荐答案

我几乎总是喜欢本地变量解决方案。

I'd nearly always prefer the local variable solution.

单个局部变量需要4或8个字节。它是一个参考,没有递归,所以让我们忽略它。

A single local variable costs 4 or 8 bytes. It's a reference and there's no recursion, so let's ignore it.

如果这是一个简单的吸气剂,JVM可以自己记忆,所以没有区别。如果这是一个无法优化的昂贵电话,手动记忆会使其更快。

If this is a simple getter, the JVM can memoize it itself, so there's no difference. If it's a expensive call which can't be optimized, memoizing manually makes it faster.

关注 DRY 原则。在你的情况下,它几乎不重要,因为局部变量名称与字符方式一样,只要方法调用,但对于任何更复杂的,它的可读性,因为你没有找到两个表达式之间的10个差异。如果你知道它们是相同的,那么使用局部变量清楚。

Follow the DRY principle. In your case it hardly matters as the local variable name is character-wise as about as long as the method call, but for anything more complicated, it's readability as you don't have to find the 10 differences between the two expressions. If you know they're the same, so make it clear using the local variable.

想象一下您的 SelectItem 不接受 null s并且您的程序是多线程的。 listType.getDescription()的值可能会在此期间发生变化并且您已经被烤了。

Imagine your SelectItem does not accept nulls and your program is multithreaded. The value of listType.getDescription() can change in the meantime and you're toasted.

拥有包含有趣值的局部变量是一个优势。

Having a local variable containing an interesting value is an advantage.

通过省略局部变量来获胜的唯一方法是保存一行。所以我只会在无关紧要的情况下这样做:

The only thing to win by omitting the local variable is saving one line. So I'd do it only in cases when it really doesn't matter:


  • 非常短的表达

  • 没有可能的并发修改

  • 简单私人最终获取者

这篇关于在对象上调用getter而不是将其存储为局部变量(内存占用,性能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆