Android的性能 - “避免内部getter / setter方法​​” [英] Android Performance - 'Avoid Internal Getters/Setters'

查看:239
本文介绍了Android的性能 - “避免内部getter / setter方法​​”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚才看了这个就开发站点:

避免内部getter / setter方法​​

  

在像C母语++是很常见的做法是使用getter方法​​(例如I = getCount将()),而不是直接访问字段(I = mCount)。这是一个很好的习惯,对于C ++,因为编译器通常可以内联访问,如果你需要限制或调试现场访问,您可以随时添加code。

     

在Android上,这是一个坏主意。虚拟方法调用是昂贵,远远超过实例字段查找。这是合理的,遵循共同的面向对象的编程实践,并有getter和setter的公共接口,而是一个类中,你应该总是直接访问字段。

     

如果没有JIT,直接字段访问大约3倍比调用一个简单的getter更快。随着JIT(其中直接字段访问是一样便宜访问本地),直接字段访问约7倍比调用一个简单的getter更快。这是Froyo的事实,但将提高在未来,当JIT内联getter方法​​。

因此​​,它是说你会使用类中字段访问:

 公共类的MyObject {

    公共对象innerObject; //这将是私人,如果我用一个getter

    公共无效doSomeStuff(){
          如果(innerObject){//在这样的类访问
                 // ....
          }
    }

    公共对象getInnerObject(){//这将是如果我是使用字段访问中删除
         返回innerObject;
    }
 }
 

但是,大约从其他对象访问什么

 公共类SecondObject {

      公共无效doSecondSomething(){
                为MyObject OB =新的MyObject();
                对象内;

                //这是我的问题基本上是(从安卓性能的角度来看)
                内= ob.getInnerObject();
                // 要么
                内部= b.innerObject

       }

 }
 

解决方案

使用内部getter和setter方法​​的性能损失也适用于外部的getter和setter。

然而,在外壳的getter和setter在其他领域显著的利益;例如preserving封装,减少有害耦合,使您的code更容易维护,等等。因此,人们普遍认为的最佳实践的使用getter和setter尽管性能损失,这可能产生。

的性能损失是<打击>当前一代较旧的Andr​​oid JIT编译器限制的结果。这种情况<打击>是一定要提高已与姜饼改善。 (参考 - http://stackoverflow.com/a/4930538/139985 ......并注意谁写的答案! )

在一般情况下,这是一个坏主意,调你的$ C $下劣质的平台,尤其是如果有一个合理的机会,更好的是在酝酿之中。

Just read this on the dev site:

Avoid Internal Getters/Setters

In native languages like C++ it's common practice to use getters (e.g. i = getCount()) instead of accessing the field directly (i = mCount). This is an excellent habit for C++, because the compiler can usually inline the access, and if you need to restrict or debug field access you can add the code at any time.

On Android, this is a bad idea. 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. This is true in Froyo, but will improve in the future when the JIT inlines getter methods.

So is it saying you would use field access within the class:

 public class MyObject {

    public Object innerObject; // This would be private if I was using a getter

    public void doSomeStuff(){
          if(innerObject){        // Within class access like this
                 // ....
          }
    }

    public Object getInnerObject(){  // This would be removed if I was using field access
         return innerObject;
    }
 }

But what about access from another object?:

 public class SecondObject {

      public void doSecondSomething(){
                MyObject ob = new MyObject();
                Object inner;

                //This is my question basically (from an Android performance perspective)   
                inner = ob.getInnerObject();
                // OR 
                inner = b.innerObject

       }

 }

解决方案

The performance hit of using internal getters and setters also applies to external getters and setters.

However, in the external case the getters and setters have significant benefits in other areas; e.g. preserving encapsulation, reducing harmful coupling, making your code more maintainable, and so on. So, it is generally regarded as best practice to use getters and setters despite the performance hit that this may incur.

The performance hit is a result of limitations of the current generation of older Android JIT compilers. This situation is sure to improve has improved with Gingerbread. (Reference - http://stackoverflow.com/a/4930538/139985 ... and note who wrote that answer!)

In general, it is a bad idea to "tune" your code for an inferior platform, especially if there is a reasonable chance that a better one is in the offing.

这篇关于Android的性能 - “避免内部getter / setter方法​​”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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