gpt4 book ai didi

java - 我们是否应该通过 Android 平台中的接口(interface)来引用对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:46:59 25 4
gpt4 key购买 nike

我采纳了 Joshua Bloch 的 Effective Java 给出的建议,第 52 条:通过接口(interface)引用对象

但是,在Android自带的大部分示例代码中,我发现下面的代码很常见。

private ArrayList<Integer> mPhotos = new ArrayList<Integer>();

我理解这是出于性能优化的目的,因为下面的代码会变慢。

private List<Integer> mPhotos = new ArrayList<Integer>();

但是,这样的优化技术还有效吗?好像我从 http://developer.android.com/guide/practices/design/performance.html

On devices without a JIT, it is true that invoking methods via a variable with an exact type rather than an interface is slightly more efficient. (So, for example, it was cheaper to invoke methods on a HashMap map than a Map map, even though in both cases the map was a HashMap.) It was not the case that this was 2x slower; the actual difference was more like 6% slower. Furthermore, the JIT makes the two effectively indistinguishable.

我们是否需要假设我们的设备没有 JIT,并且引用没有接口(interface)的对象?或者,我们应该采纳 Joshua Bloch 的建议吗?

最佳答案

从 Android 2.2 开始,Dalvik VM(运行作为 Java 源代码结果的 Dalvik 字节码)具有即时编译器 (JIT)。

我不知道这个特定的优化是否是 JIT 的一部分,但它应该可以在实际设备上进行测试。

如果您的目标是 pre-2.2 设备并且调用中的 6% 开销(不要与应用程序整体 6% 的减速相混淆!)对您的应用程序产生严重影响,那么该优化可能是值得的。

关于java - 我们是否应该通过 Android 平台中的接口(interface)来引用对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6611293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com