gpt4 book ai didi

Java:instanceof vs 类名切换性能

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:35 25 4
gpt4 key购买 nike

我对确定对象类型的性能问题很感兴趣,并编写了一些具有 2 种确定方式的基准。

有人能解释一下为什么使用 instanceof 的变体比使用字符串切换类名的变体快 350 倍吗?

代码:

class A {}
class B extends A {}

public class InstanceOfBenchmark {
public static final Object a = new A();

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void testInstanceOf()
{
if (a instanceof B) {}
else if (a instanceof String) {}
else if (a instanceof ArrayList) {}
else if (a instanceof HashMap) {}
else if (a instanceof HashSet) {}
else if (a instanceof A);
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public void testName() {
String class_name = a.getClass().getSimpleName();

switch (class_name) {
case ("B") :
case ("String") :
case ("ArrayList") :
case ("HashMap") :
case ("HashSet") :
case ("A") :
}
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(InstanceOfBenchmark.class.getSimpleName())
.warmupIterations(20)
.measurementIterations(100)
.forks(1)
.build();

new Runner(opt).run();
}
}

结果:

Benchmark                            Mode  Cnt     Score    Error   Units
InstanceOfBenchmark.testInstanceOf thrpt 100 3482.001 ± 25.447 ops/us
InstanceOfBenchmark.testName thrpt 100 10.579 ± 0.078 ops/us

我也运行了 200 次预热和 2000 次迭代的测试,结果是一样的。

最佳答案

字符串版本的字节码:

   0: aload_0
1: invokevirtual #2 // Method java/lang/Object.getClass:()Ljava/lang/Class;
4: invokevirtual #3 // Method java/lang/Class.getSimpleName:()Ljava/lang/Str
7: astore_1
8: aload_1
9: astore_2
10: iconst_m1
11: istore_3
12: aload_2
13: invokevirtual #4 // Method java/lang/String.hashCode:()I
16: lookupswitch { // 6
-1932803762: 118
-1932797868: 132
-1808118735: 90
65: 146
66: 76
578806391: 104
default: 157
}
76: aload_2
77: ldc #5 // String B
79: invokevirtual #6 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
82: ifeq 157
85: iconst_0
86: istore_3
87: goto 157
90: aload_2
91: ldc #7 // String String
93: invokevirtual #6 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
96: ifeq 157
99: iconst_1
100: istore_3
101: goto 157
104: aload_2
105: ldc #8 // String ArrayList
107: invokevirtual #6 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
110: ifeq 157
113: iconst_2
114: istore_3
115: goto 157
118: aload_2
119: ldc #9 // String HashMap
121: invokevirtual #6 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
124: ifeq 157
127: iconst_3
128: istore_3
129: goto 157
132: aload_2
133: ldc #10 // String HashSet
135: invokevirtual #6 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
138: ifeq 157
141: iconst_4
142: istore_3
143: goto 157
146: aload_2
147: ldc #11 // String A
149: invokevirtual #6 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
152: ifeq 157
155: iconst_5
156: istore_3
157: iload_3
158: tableswitch { // 0 to 5
0: 196
1: 196
2: 196
3: 196
4: 196
5: 196
default: 196
}
196: return

instanceof 版本的字节码:

  0: aload_0
1: instanceof #12 // class B
4: ifeq 10
7: goto 57
10: aload_0
11: instanceof #13 // class java/lang/String
14: ifeq 20
17: goto 57
20: aload_0
21: instanceof #14 // class java/util/ArrayList
24: ifeq 30
27: goto 57
30: aload_0
31: instanceof #15 // class java/util/HashMap
34: ifeq 40
37: goto 57
40: aload_0
41: instanceof #16 // class java/util/HashSet
44: ifeq 50
47: goto 57
50: aload_0
51: instanceof #17 // class A
54: ifeq 57
57: return

instanceof 更快,您是否仍然感到惊讶?操作次数明显减少。

关于Java:instanceof vs 类名切换性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50946488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com