简体   繁体   English

直接访问静态字段而不是调用静态getter方法,它更快吗?

[英]Access static fields directly instead of calling a static getter method, is it faster?

I'm writing an Android app and I have a class that generates and maintains some fixed URL's that can occasionally change. 我正在编写一个Android应用程序,我有一个类可以生成并维护一些偶尔可以更改的固定URL。 I keep them all in one class called UrlUtils: 我将它们全部保存在一个名为UrlUtils的类中:

public class UrlUtils {
  private static String sUrlBase = "http://google.com";

  /**
   * Called occasionally
   */
  public static void refreshUrlBases() {
        sUrlBase = "http://yahoo.com/" + new Random().nextInt();
  }

   public static String getUrlBase() {
        return sUrlBase;
   }
}

I have to make a lot of calls to getUrlBase() above, so I was thinking about making sUrlBase public and access it directly. 我必须在上面调用getUrlBase() ,所以我考虑将sUrlBase公开并直接访问它。 Will this approach be better in terms of performance? 这种方法在性能方面会更好吗?

Yes, for performance reasons you should avoid use of getters and setters. 是的,出于性能原因,您应该避免使用getter和setter。 Here is a tip from android " Designing for performance " doc. 这是Android“ 设计性能 ”文档的提示。

Without a JIT, direct field access is about 3x faster than invoking a trivial getter. 没有JIT,直接字段访问速度比调用一个简单的getter快约3倍。 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. 使用JIT(直接字段访问与访问本地一样便宜),直接字段访问比调用一个简单的getter快约7倍。 This is true in Froyo, but will improve in the future when the JIT inlines getter methods. 在Froyo中也是如此,但是当JIT内联getter方法时,将来会有所改进。

If you are not going to change your url more frequently, then instead of getter/setter method I will say that you can keep static directly 如果你不打算更频繁地更改你的网址,那么我会说你可以直接保持static而不是getter/setter方法

public static String google_url = "http://google.com";
public static String yahoo_url  = "http://yahoo.com";

In keeping static method, it may happen that due to some problem static setter values are removed (reset to original). 在保持static方法时,可能会由于某些问题而removed static setter值(重置为原始值)。 So, in that case it will return you the original constant of static value. 因此,在这种情况下,它将返回static值的original常量。

UPDATE: 更新:

If your are going to update your URL dynamically then static method would prove to be better option. 如果您要dynamically更新URL那么static method将被证明是更好的选择。

    public static String sUrlBase = "http://google.com"; 

    public static String getsUrlBase() {
        return sUrlBase;
    }
    public static void setsUrlBase(String sUrlBase) {
        this.sUrlBase = sUrlBase;
    }

It makes no difference. 没什么区别。 You should use whatever method is easiest for you. 您应该使用最简单的方法。 Usually you should use getters and setters for readability and code practices. 通常,您应该使用getter和setter来实现可读性和代码实践。

Normally I would not worry too much about performance for small simple data like URL as in this case, and so for that matter whether it is direct field access or access using methods will not bother me much. 通常情况下,我不会过分担心像URL这样的小型简单数据的性能,因此无论是直接字段访问还是使用方法访问都不会让我感到烦恼。 The case where it will makes difference for me is when frequency of getter calls is so high that it is affecting overall response. 这对我来说有所不同的情况是,当getter调用的频率很高时,它会影响整体响应。 My concern will be regarding changing the value accidentally [bugs :( ] since the field has public static access. When accessed through getter/setter at least there is one place where I can do some checks. 我担心的是意外更改值[bugs :(],因为该字段具有公共静态访问权限。当通过getter / setter访问时,至少有一个地方可以进行一些检查。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM