Skip to content

Instantly share code, notes, and snippets.

@arturmkrtchyan
Last active August 29, 2015 14:13
Show Gist options
  • Save arturmkrtchyan/472147fc312adcd96a8d to your computer and use it in GitHub Desktop.
Save arturmkrtchyan/472147fc312adcd96a8d to your computer and use it in GitHub Desktop.
JVM tableswitch vs lookupswitch
private static void enumSwitch();
Code:
0: getstatic #2 // Field DayOfWeek.FRIDAY:LDayOfWeek;
3: astore_0
4: iconst_0
5: istore_1
6: getstatic #3 // Field Switch$1.$SwitchMap$DayOfWeek:[I
9: aload_0
10: invokevirtual #4 // Method DayOfWeek.ordinal:()I
13: iaload
14: tableswitch { // 1 to 7
1: 56
2: 61
3: 66
4: 71
5: 76
6: 81
7: 87
default: 90
}
56: iconst_1
57: istore_1
58: goto 90
61: iconst_2
62: istore_1
63: goto 90
66: iconst_3
67: istore_1
68: goto 90
71: iconst_4
72: istore_1
73: goto 90
76: iconst_5
77: istore_1
78: goto 90
81: bipush 6
83: istore_1
84: goto 90
87: bipush 7
89: istore_1
90: return
public class Switch {
private static void enumSwitch() {
final DayOfWeek day = DayOfWeek.FRIDAY;
int result = 0;
switch (day) {
case MONDAY:
result = 1;
break;
case TUESDAY:
result = 2;
break;
case WEDNESDAY:
result = 3;
break;
case THURSDAY:
result = 4;
break;
case FRIDAY:
result = 5;
break;
case SATURDAY:
result = 6;
break;
case SUNDAY:
result = 7;
break;
}
}
}
enum DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
}
private static void nonSequentialIntSwitch();
Code:
0: iconst_0
1: istore_1
2: iconst_4
3: tableswitch { // 4 to 13
4: 56
5: 61
6: 78
7: 78
8: 78
9: 66
10: 78
11: 71
12: 78
13: 76
default: 78
}
56: iconst_5
57: istore_1
58: goto 78
61: iconst_4
62: istore_1
63: goto 78
66: iconst_3
67: istore_1
68: goto 78
71: iconst_2
72: istore_1
73: goto 78
76: iconst_1
77: istore_1
78: return
public class Switch {
private static void nonSequentialIntSwitch() {
final int status = 4;
int result = 0;
switch (status) {
case 4:
result = 5;
break;
case 5:
result = 4;
break;
case 9:
result = 3;
break;
case 11:
result = 2;
break;
case 13:
result = 1;
break;
}
}
}
private static void sequentialIntSwitch();
Code:
0: iconst_0
1: istore_1
2: iconst_4
3: tableswitch { // 1 to 5
1: 41
2: 36
3: 46
4: 56
5: 51
default: 58
}
36: iconst_5
37: istore_1
38: goto 58
41: iconst_4
42: istore_1
43: goto 58
46: iconst_3
47: istore_1
48: goto 58
51: iconst_2
52: istore_1
53: goto 58
56: iconst_1
57: istore_1
58: return
public class Switch {
private static void sequentialIntSwitch() {
final int status = 4;
int result = 0;
switch (status) {
case 2:
result = 5;
break;
case 1:
result = 4;
break;
case 3:
result = 3;
break;
case 5:
result = 2;
break;
case 4:
result = 1;
break;
}
}
}
private static void sparseIntSwitch();
Code:
0: iconst_0
1: istore_1
2: sipush 200
5: lookupswitch { // 5
100: 76
200: 71
300: 66
400: 61
500: 56
default: 78
}
56: iconst_5
57: istore_1
58: goto 78
61: iconst_4
62: istore_1
63: goto 78
66: iconst_3
67: istore_1
68: goto 78
71: iconst_2
72: istore_1
73: goto 78
76: iconst_1
77: istore_1
78: return
public class Switch {
private static void sparseIntSwitch() {
final int status = 200;
int result = 0;
switch (status) {
case 500:
result = 5;
break;
case 400:
result = 4;
break;
case 300:
result = 3;
break;
case 200:
result = 2;
break;
case 100:
result = 1;
break;
}
}
}
private static void stringSwitch();
Code:
0: iconst_0
1: istore_1
2: ldc #2 // String MONDAY
4: astore_2
5: iconst_m1
6: istore_3
7: aload_2
8: invokevirtual #3 // Method java/lang/String.hashCode:()I
11: lookupswitch { // 7
-2015173360: 76
-1940284966: 118
-1837857328: 160
-1331574855: 146
-259361235: 90
-114841802: 104
2082011487: 132
default: 172
}
76: aload_2
77: ldc #2 // String MONDAY
79: invokevirtual #4 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
82: ifeq 172
85: iconst_0
86: istore_3
87: goto 172
90: aload_2
91: ldc #5 // String TUESDAY
93: invokevirtual #4 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
96: ifeq 172
99: iconst_1
100: istore_3
101: goto 172
104: aload_2
105: ldc #6 // String WEDNESDAY
107: invokevirtual #4 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
110: ifeq 172
113: iconst_2
114: istore_3
115: goto 172
118: aload_2
119: ldc #7 // String THURSDAY
121: invokevirtual #4 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
124: ifeq 172
127: iconst_3
128: istore_3
129: goto 172
132: aload_2
133: ldc #8 // String FRIDAY
135: invokevirtual #4 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
138: ifeq 172
141: iconst_4
142: istore_3
143: goto 172
146: aload_2
147: ldc #9 // String SATURDAY
149: invokevirtual #4 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
152: ifeq 172
155: iconst_5
156: istore_3
157: goto 172
160: aload_2
161: ldc #10 // String SUNDAY
163: invokevirtual #4 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
166: ifeq 172
169: bipush 6
171: istore_3
172: iload_3
173: tableswitch { // 0 to 6
0: 216
1: 221
2: 226
3: 231
4: 236
5: 241
6: 247
default: 250
}
216: iconst_1
217: istore_1
218: goto 250
221: iconst_2
222: istore_1
223: goto 250
226: iconst_3
227: istore_1
228: goto 250
231: iconst_4
232: istore_1
233: goto 250
236: iconst_5
237: istore_1
238: goto 250
241: bipush 6
243: istore_1
244: goto 250
247: bipush 7
249: istore_1
250: return
public class Switch {
private static void stringSwitch() {
final String day = "MONDAY";
int result = 0;
switch (day) {
case "MONDAY":
result = 1;
break;
case "TUESDAY":
result = 2;
break;
case "WEDNESDAY":
result = 3;
break;
case "THURSDAY":
result = 4;
break;
case "FRIDAY":
result = 5;
break;
case "SATURDAY":
result = 6;
break;
case "SUNDAY":
result = 7;
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment