Are Java static calls more or less expensive than non-static calls?

前端 未结 12 2350
夕颜
夕颜 2020-11-27 11:57

Is there any performance benefit one way or another? Is it compiler/VM specific? I am using Hotspot.

相关标签:
12条回答
  • 2020-11-27 12:38

    It is unbelievably unlikely that any difference in the performance of static versus non-static calls is making a difference in your application. Remember that "premature optimization is the root of all evil".

    0 讨论(0)
  • 2020-11-27 12:38

    As Jon notes, static methods can't be overridden, so simply invoking a static method may be -- on a sufficiently naive Java runtime -- faster than invoking an instance method.

    But then, even assuming you're at the point where you care about messing up your design to save a few nanoseconds, that just brings up another question: will you need method overriding yourself? If you change your code around to make an instance method into a static method to save a nanosecond here and there, and then turn around and implement your own dispatcher on top of that, yours is almost certainly going to be less efficient than the one built into your Java runtime already.

    0 讨论(0)
  • 2020-11-27 12:39

    I would like to add to the other great answers here that it also depends on your flow, for example:

    Public class MyDao {
    
       private String sql = "select * from MY_ITEM";
    
       public List<MyItem> getAllItems() {
           springJdbcTemplate.query(sql, new MyRowMapper());
       };
    };
    

    Pay attention that you create a new MyRowMapper object per each call.
    Instead, I suggest to use here a static field.

    Public class MyDao {
    
       private static RowMapper myRowMapper = new MyRowMapper();
       private String sql = "select * from MY_ITEM";
    
       public List<MyItem> getAllItems() {
           springJdbcTemplate.query(sql, myRowMapper);
       };
    };
    
    0 讨论(0)
  • 2020-11-27 12:46

    In theory, less expensive.

    Static initialization is going to be done even if you create an instance of the object, whereas static methods will not do any initialization normally done in a constructor.

    However, I haven't tested this.

    0 讨论(0)
  • 2020-11-27 12:56

    For the decision if a method should be static, the performance aspect should be irrelevant. If you have a performance problem, making lots of methods static isn't going to save the day. That said, static methods are almost certainly not slower than any instance method, in most cases marginally faster:

    1.) Static methods are not polymorphic, so the JVM has less decisions to make to find the actual code to execute. This is a moot point in the Age of Hotspot, since Hotspot will optimize instance method calls that have only one implementation site, so they will perform the same.

    2.) Another subtle difference is that static methods obviously have no "this" reference. This results in a stack frame one slot smaller than that of an instance method with the same signature and body ("this" is put in slot 0 of the local variables on the bytecode level, whereas for static methods slot 0 is used for the first parameter of the method).

    0 讨论(0)
  • 2020-11-27 12:57

    As previous posters have said: This seems like a premature optimization.

    However, there is one difference (a part from the fact that non-static invokations require an additional push of a callee-object onto the operand stack):

    Since static methods can't be overridden, there will not be any virtual lookups in runtime for a static method call. This may result in an observable difference under some circumstances.

    The difference on a byte-code level is that a non-static method call is done through INVOKEVIRTUAL, INVOKEINTERFACE or INVOKESPECIAL while a static method call is done through INVOKESTATIC.

    0 讨论(0)
提交回复
热议问题