java - 如果最终确定,公共(public) getter 和 setter 是否适用于 Android? Java 中的最终正确性与 C++ 中的常量正确性一样重要吗?

标签 java android virtual final

<分区>

我正在读这个:http://developer.android.com/training/articles/perf-tips.html

特别是关于内部 getter 和 setter 的:

Virtual method calls are expensive, much more so than instance field lookups. It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly.

Without a JIT, direct field access is about 3x faster than invoking a trivial getter. With the JIT (where direct field access is as cheap as accessing a local), direct field access is about 7x faster than invoking a trivial getter.

它提到“虚拟方法调用”,可能也指公共(public)方法。我的类中有很多方法不应被覆盖。例如:

public class Something {
    private float m_Float = 0.0f;
    public float getFloat () {
        return m_Float;
    }
}

我总是希望“getFloat()”返回“m_Float”,即使在派生类中也是如此。将方法标记为“最终”是否会提高 Android 设备的性能?即使没有,最终正确性与常量正确性一样重要吗?比如,当同事忘记最终正确性时,Java 程序员会生气吗?

如果将方法标记为 final 可以提高性能,那么下面的性能增益是否会被抵消?

public class Base {
    public int getRandomNumber () {
        return 4; //chosen by fair dice roll.
                  //guaranteed to be random.
    }
}
public class Derived extends Base {
    public final int getRandomNumber () {
        return 6; //chosen by second fair dice roll.
                  //guaranteed to be more random.
    }
}

目前我对优化我的代码并不感兴趣,但我对最终正确性位感兴趣。我不熟悉 Java 所关注的标准约定。

[编辑]

好的,所以,上面给出了这个链接作为可能的答案:Android Performance - 'Avoid Internal Getters/Setters'

标记为答案链接的回复:What optimizations can I expect from Dalvik and the Android toolchain?

现在似乎内联了简单的 getter 和 setter。

In Gingerbread we added simple inlining for getters/setters. Since the underlying JIT frontend is still simple trace based, if the callee has branches in there it won't be inlined. But the inline cache mechanism is implemented so that virtual getters/setters can be inlined without problems.

在评论中,有人问:

(3) Should I declare my methods final wherever possible? Or does that still count as a virtual callsite?

这是回复:

(3) Yes please

所以,有了这里的评论等等,我想说一切都解决了。 (除了何时 final 应该与方法一起使用;但这可能很快就会得到回答。我有一个信念,但我正在等待它得到验证或有理由被驳斥,所以..)

[编辑]

此外,这是否意味着 Android 文档。关于性能提示是..过时了?

最佳答案

就个人而言,我建议不要将final 用于常量之外的任何东西。在多年的开发中,除了常量的 static final ... 之外,我真的没有看到 final 有什么好的用途。 final 关键字在测试时特别令人烦恼 - java.net.URL 作为 final class 需要使用类似 PowerMock 的东西如果您真的想要高测试覆盖率。

如有疑问,请制定切合实际的性能测试并对两种情况进行分析。如果没有明显区别,则不要在代码中添加像 final 这样的限制。语句如下:

direct field access is about 3x faster than invoking a trivial getter

没有一些实数是毫无意义的。如果调用一个普通的 getter 只需要 20 微秒,那么将成本增加到 60 微秒是没有意义的。对于移动应用程序,getter 和 setter 不会导致性能问题。

始终编码的正确性第一,可读性第二,其次是可维护性。

关于java - 如果最终确定,公共(public) getter 和 setter 是否适用于 Android? Java 中的最终正确性与 C++ 中的常量正确性一样重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17180237/

相关文章:

java - Java 中的 Sprintf 等价物

java - 如何设置 get CreatedAt(); android textview 中的日期对象?

java - 从 hql 结果中选择单个对象

android - 禁用在 Android Lollipop 中隐藏通知的可能性

绑定(bind)到单个网站订阅的 Android/iOS 无限应用

c++ - 需要覆盖逆变变通方法

java - 为什么 “dist/”目录下没有manifest.mf,为什么jar文件不工作?

c++ - 是否可以在 C++ 类中声明一个虚拟静态常量值?

c++ - 如何修复编译器错误 "class has no member named X"?

Android - 水平线上的形状