指令集:invoke*

invokevirtual:类对象的方法
invokespecial:私有方法,构造函数,super类的方法
invokestatic:静态方法
invokeinterface:接口方法

示例代码

public class SimpleInvokeBenchMark implements Itest {
 
 @Override
 public void test() {
  
 }
 
 private void testPrivate() {
  
 }
 
 public static void testStatic() {
  
 }
 
 public static void main(String[] args) {
  SimpleInvokeBenchMark simpleInvokeBenchMark = new SimpleInvokeBenchMark();
  Itest iface = simpleInvokeBenchMark;
  //warm up
  int count = 10000000;  
        for (int i = 0; i < count; i++) {  
         simpleInvokeBenchMark.test();  
         simpleInvokeBenchMark.testPrivate();  
         SimpleInvokeBenchMark.testStatic();  
            iface.test();  
        }  
        
        //start
        count = 100000000;  
        
        //invokevirtual
        long startTime = System.currentTimeMillis();  
        for (int i = 0; i < count; i++) {  
            simpleInvokeBenchMark.test();  
        }  
        System.out.println("invokevirtual:   " + (System.currentTimeMillis() - startTime));  
  
        //invokespecial
        startTime = System.currentTimeMillis();  
        for (int i = 0; i < count; i++) {  
            simpleInvokeBenchMark.testPrivate();  
        }  
        System.out.println("invokespecial:   " + (System.currentTimeMillis() - startTime));  
  
        //invokestatic
        startTime = System.currentTimeMillis();  
        for (int i = 0; i < count; i++) {  
            SimpleInvokeBenchMark.testStatic();  
        }  
        System.out.println("invokestatic:    " + (System.currentTimeMillis() - startTime));  
  
        //invokeinterface
        startTime = System.currentTimeMillis();  
        for (int i = 0; i < count; i++) {  
            iface.test();  
        }  
        System.out.println("invokeinterface: " + (System.currentTimeMillis() - startTime)); 
 }
}

字节码

public class test.zk.instrumentset.SimpleInvokeBenchMark implements test.zk.instrumentset.Itest {
  public test.zk.instrumentset.SimpleInvokeBenchMark();
    Code:
       0: aload_0
       1: invokespecial #10                 // Method java/lang/Object."":()V
       4: return

  public void test();
    Code:
       0: return

  public static void testStatic();
    Code:
       0: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #1                  // class test/zk/instrumentset/SimpleInvokeBenchMark
       3: dup
       4: invokespecial #21                 // Method "":()V
       7: astore_1
       8: aload_1
       9: astore_2
      10: ldc           #22                 // int 10000000
      12: istore_3
      13: iconst_0
      14: istore        4
      16: goto          39
      19: aload_1
      20: invokevirtual #23                 // Method test:()V
      23: aload_1
      24: invokespecial #25                 // Method testPrivate:()V
      27: invokestatic  #27                 // Method testStatic:()V
      30: aload_2
      31: invokeinterface #29,  1           // InterfaceMethod test/zk/instrumentset/Itest.test:()V
      36: iinc          4, 1
      39: iload         4
      41: iload_3
      42: if_icmplt     19
      45: ldc           #30                 // int 100000000
      47: istore_3
      48: invokestatic  #31                 // Method java/lang/System.currentTimeMillis:()J
      51: lstore        4
      53: iconst_0
      54: istore        6
      56: goto          66
      59: aload_1
      60: invokevirtual #23                 // Method test:()V
      63: iinc          6, 1
      66: iload         6
      68: iload_3
      69: if_icmplt     59
      72: getstatic     #37                 // Field java/lang/System.out:Ljava/io/PrintStream;
      75: new           #41                 // class java/lang/StringBuilder
      78: dup
      79: ldc           #43                 // String invokevirtual:
      81: invokespecial #45                 // Method java/lang/StringBuilder."":(Ljava/lang/String;)V
      84: invokestatic  #31                 // Method java/lang/System.currentTimeMillis:()J
      87: lload         4
      89: lsub
      90: invokevirtual #48                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
      93: invokevirtual #52                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      96: invokevirtual #56                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      99: invokestatic  #31                 // Method java/lang/System.currentTimeMillis:()J
     102: lstore        4
     104: iconst_0
     105: istore        6
     107: goto          117
     110: aload_1
     111: invokespecial #25                 // Method testPrivate:()V
     114: iinc          6, 1
     117: iload         6
     119: iload_3
     120: if_icmplt     110
     123: getstatic     #37                 // Field java/lang/System.out:Ljava/io/PrintStream;
     126: new           #41                 // class java/lang/StringBuilder
     129: dup
     130: ldc           #61                 // String invokespecial:
     132: invokespecial #45                 // Method java/lang/StringBuilder."":(Ljava/lang/String;)V
     135: invokestatic  #31                 // Method java/lang/System.currentTimeMillis:()J
     138: lload         4
     140: lsub
     141: invokevirtual #48                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
     144: invokevirtual #52                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     147: invokevirtual #56                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
     150: invokestatic  #31                 // Method java/lang/System.currentTimeMillis:()J
     153: lstore        4
     155: iconst_0
     156: istore        6
     158: goto          167
     161: invokestatic  #27                 // Method testStatic:()V
     164: iinc          6, 1
     167: iload         6
     169: iload_3
     170: if_icmplt     161
     173: getstatic     #37                 // Field java/lang/System.out:Ljava/io/PrintStream;
     176: new           #41                 // class java/lang/StringBuilder
     179: dup
     180: ldc           #63                 // String invokestatic:
     182: invokespecial #45                 // Method java/lang/StringBuilder."":(Ljava/lang/String;)V
     185: invokestatic  #31                 // Method java/lang/System.currentTimeMillis:()J
     188: lload         4
     190: lsub
     191: invokevirtual #48                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
     194: invokevirtual #52                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     197: invokevirtual #56                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
     200: invokestatic  #31                 // Method java/lang/System.currentTimeMillis:()J
     203: lstore        4
     205: iconst_0
     206: istore        6
     208: goto          220
     211: aload_2
     212: invokeinterface #29,  1           // InterfaceMethod test/zk/instrumentset/Itest.test:()V
     217: iinc          6, 1
     220: iload         6
     222: iload_3
     223: if_icmplt     211
     226: getstatic     #37                 // Field java/lang/System.out:Ljava/io/PrintStream;
     229: new           #41                 // class java/lang/StringBuilder
     232: dup
     233: ldc           #65                 // String invokeinterface:
     235: invokespecial #45                 // Method java/lang/StringBuilder."":(Ljava/lang/String;)V
     238: invokestatic  #31                 // Method java/lang/System.currentTimeMillis:()J
     241: lload         4
     243: lsub
     244: invokevirtual #48                 // Method java/lang/StringBuilder.append:(J)Ljava/lang/StringBuilder;
     247: invokevirtual #52                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
     250: invokevirtual #56                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
     253: return
}

BenchMark(ms)

count=100000000
invokevirtual: 3
invokespecial: 6
invokestatic: 6
invokeinterface: 5

count = 1000000000L
invokevirtual: 348
invokespecial: 682
invokestatic: 351
invokeinterface: 351

参考

java虚拟机规范-invokespecial
java虚拟机规范-invokevirtual
java虚拟机规范-invokestatic
java虚拟机规范-invokeinterface

你可能感兴趣的:(指令集:invoke*)