简体   繁体   English

静态变量与实例变量之间的性能差异

[英]Performance difference between static and instance variables

An instance variable is one per Object, every object has its own copy of instance variable. 一个实例变量是每个对象一个,每个对象都有自己的实例变量副本。

A static variable is one per Class, every object of that class shares the same Static variable. 静态变量是每个类一个,该类的每个对象共享相同的静态变量。

class MyStaticClass{
  private static int myStaticInt;

  public static int getMyStaticInt() {return myStaticInt;}
}

class MyInstanceClass{
  private int myNonStaticInt;

  public int getMyNonStaticInt() {return myNonStaticInt;}
}

Is there a performance difference between either? 两者之间是否有性能差异? Is it more expensive to call one over the other? 打电话给另一个人更昂贵吗?

int i = MyStaticClass.getMyStaticInt();

OR: 要么:

int i = new MyInstanceClass().getMyNonStaticInt();

It's not a matter of performance. 这不是性能问题。 static and instance variables have a different purpose. 静态变量和实例变量具有不同的用途。

Using 运用

int i = new MyInstatnceClass().getMyNonStaticInt();

is almost certainly useless, since each time you call new MyInstatnceClass() you create a new MyInstatnceClass instance, having a new myNonStaticInt instance variable. 几乎肯定是没有用的,因为每次调用new MyInstatnceClass()您都会创建一个新的MyInstatnceClass实例,并具有一个新的myNonStaticInt实例变量。 Since you are not keep a reference to the created instance, you cannot retrieve the same instance variable twice, which makes it useless. 由于您没有保留对创建实例的引用,因此无法两次检索相同的实例变量,这使它无用。

If you need a single copy of a variable to be shared across all instances of the class, static variable is the way to go. 如果您需要在类的所有实例之间共享变量的单个副本,则可以采用static变量。

That said, the latter call is also more expansive, since it involves creation and initialization of an instance of your MyInstatnceClass class (in addition to loading and initialzing the class if it's the first access that class). 就是说,后一个调用也更广泛,因为它涉及创建和初始化MyInstatnceClass类的实例(除了加载和初始化该类(如果它是对该类的首次访问)之外)。

On the other hand, MyStaticClass.getMyStaticInt() only loads and initializes the class MyStaticClass if it's the first access of that class. 另一方面,如果MyStaticClass.getMyStaticInt()是该类的首次访问,则仅加载并初始化该类MyStaticClass It doesn't have to create any instance of that class. 它不必创建该类的任何实例。

Since instance methods can be polymorphically overridden , a very naive JVM implementation must at least initially use a virtual mehod table to find the appropriate method to call. 由于实例方法可以被多态覆盖 ,因此非常幼稚的JVM实现必须至少最初使用虚拟方法来查找要调用的适当方法。 Classes themselves, however are not polymorphic and class methods cannot be overridden. 类本身不是多态的,并且类方法不能被覆盖。 Due to this, they have a simpler lookup mechanism . 因此, 它们具有更简单的查找机制

However , real-world JVMs are incredibly smart and can tell which methods are never overridden and optimize this lookup away. 但是 ,现实世界中的JVM非常智能,可以告诉哪些方法永远不会被覆盖并优化查找。 In other words, in all but the most contrived instances with non-existent JVMs will there be a difference in performance. 换句话说,除了使用虚拟机不存在的最人为设计的实例外,其他所有实例的性能都会有所不同。 Instead, use static methods to represent functionalities relevant to the entire class of objects itself rather than to a single instance thereof. 取而代之的是,使用静态方法来表示与整个对象类本身(而不是对象的单个实例)相关的功能。

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

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