Last active
October 13, 2018 17:11
-
-
Save koduki/3085ab86886e42e565eecbea5dded943 to your computer and use it in GitHub Desktop.
みんな大好きJavaの文字列結合に関して調べてみた。とりあえずコンパイラは結構仕事をしているみた。StringBuilder直より命令数少ないし、Java11だとさらなる最適化がされておる...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void checkJavacOptimizationPlus() { | |
String hello = "hello(v)"; | |
String world = "world(v)"; | |
final String HELLO = "hello(c)"; | |
final String WOLRD = "world(c)"; | |
String mixedStr2 = "hello(l)" + "world(l)" + HELLO + WOLRD + hello + world; | |
} | |
public static void checkJavacOptimizationSB() { | |
String hello = "hello(v)"; | |
String world = "world(v)"; | |
final String HELLO = "hello(c)"; | |
final String WOLRD = "world(c)"; | |
StringBuilder sb = new StringBuilder(); | |
sb.append("hello(l)"); | |
sb.append("world(l)"); | |
sb.append(HELLO); | |
sb.append(WOLRD); | |
sb.append(hello); | |
sb.append(world); | |
String sbStr = sb.toString(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void checkJavacOptimizationPlus(); | |
Code: | |
0: ldc #22 // String hello(v) | |
2: astore_0 | |
3: ldc #23 // String world(v) | |
5: astore_1 | |
6: ldc #24 // String hello(c) | |
8: astore_2 | |
9: ldc #25 // String world(c) | |
11: astore_3 | |
12: aload_0 | |
13: aload_1 | |
14: invokedynamic #29, 0 // InvokeDynamic #6:makeConcatWithConstants:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; | |
19: astore 4 | |
21: return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void checkJavacOptimizationPlus(); | |
Code: | |
0: ldc #23 // String hello(v) | |
2: astore_0 | |
3: ldc #24 // String world(v) | |
5: astore_1 | |
6: ldc #25 // String hello(c) | |
8: astore_2 | |
9: ldc #26 // String world(c) | |
11: astore_3 | |
12: new #6 // class java/lang/StringBuilder | |
15: dup | |
16: invokespecial #7 // Method java/lang/StringBuilder."<init>":()V | |
19: ldc #29 // String hello(l)world(l)hello(c)world(c) | |
21: invokevirtual #9 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
24: aload_0 | |
25: invokevirtual #9 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
28: aload_1 | |
29: invokevirtual #9 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
32: invokevirtual #11 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; | |
35: astore 4 | |
37: return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void checkJavacOptimizationSB(); | |
Code: | |
0: ldc #23 // String hello(v) | |
2: astore_0 | |
3: ldc #24 // String world(v) | |
5: astore_1 | |
6: ldc #25 // String hello(c) | |
8: astore_2 | |
9: ldc #26 // String world(c) | |
11: astore_3 | |
12: new #6 // class java/lang/StringBuilder | |
15: dup | |
16: invokespecial #7 // Method java/lang/StringBuilder."<init>":()V | |
19: astore 4 | |
21: aload 4 | |
23: ldc #30 // String hello(l) | |
25: invokevirtual #9 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
28: pop | |
29: aload 4 | |
31: ldc #31 // String world(l) | |
33: invokevirtual #9 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
36: pop | |
37: aload 4 | |
39: ldc #25 // String hello(c) | |
41: invokevirtual #9 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
44: pop | |
45: aload 4 | |
47: ldc #26 // String world(c) | |
49: invokevirtual #9 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
52: pop | |
53: aload 4 | |
55: aload_0 | |
56: invokevirtual #9 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
59: pop | |
60: aload 4 | |
62: aload_1 | |
63: invokevirtual #9 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; | |
66: pop | |
67: aload 4 | |
69: invokevirtual #11 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; | |
72: astore 5 | |
74: return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void checkJavacOptimizationPlus() { | |
String hello = "hello(v)"; | |
String world = "world(v)"; | |
String HELLO = "hello(c)"; | |
String WOLRD = "world(c)"; | |
String mixedStr2 = "hello(l)world(l)hello(c)world(c)" + hello + world; | |
} | |
public static void checkJavacOptimizationSB() { | |
String hello = "hello(v)"; | |
String world = "world(v)"; | |
String HELLO = "hello(c)"; | |
String WOLRD = "world(c)"; | |
StringBuilder sb = new StringBuilder(); | |
sb.append("hello(l)"); | |
sb.append("world(l)"); | |
sb.append("hello(c)"); | |
sb.append("world(c)"); | |
sb.append(hello); | |
sb.append(world); | |
String sbStr = sb.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment