String字符串性能优化的几种方案
String字符串是系统里最常用的类型之一,在系统中占据了很大的内存,因此,高效地使用字符串,对系统的性能有较好的提升。
针对字符串的优化,我在工作与学习过程总结了以下三种方案作分享:
1 public class test3 { 2 public static void main(String[] args) { 3 String str="ab"+"cd"+"ef"+"123"; 4 } 5 }
执行完成后,用反编译工具jad进行反编译:jad -o -a -s d.java test.class
1 // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. 2 // Jad home page: http://www.kpdus.com/jad.html 3 // Decompiler options: packimports(3) annotate 4 // Source File Name: test.java 5 package example; 6 public class test 7 { 8 public test() 9 { 10 // 0 0:aload_0 11 // 1 1:invokespecial #1 <Method void Object()> 12 // 2 4:return 13 } 14 public static void main(String args[]) 15 { 16 String str = "abcdef123"; 17 // 0 0:ldc1 #2 <String "abcdef123"> 18 // 1 2:astore_1 19 // 2 3:return 20 } 21 }
1 public class test1 { 2 public static void main(String[] args) 3 { 4 String s = "abc"; 5 String ss = "ok" + s + "xyz" + 5; 6 System.out.println(ss); 7 } 8 }
1 // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. 2 // Jad home page: http://www.kpdus.com/jad.html 3 // Decompiler options: packimports(3) annotate 4 // Source File Name: test1.java 5 6 package example; 7 8 import java.io.PrintStream; 9 10 public class test1 11 { 12 public test1() 13 { 14 // 0 0:aload_0 15 // 1 1:invokespecial #1 <Method void Object()> 16 // 2 4:return 17 } 18 public static void main(String args[]) 19 { 20 String s = "abc"; 21 // 0 0:ldc1 #2 <String "abc"> 22 // 1 2:astore_1 23 String ss = (new StringBuilder()).append("ok").append(s).append("xyz").append(5).toString(); 24 // 2 3:new #3 <Class StringBuilder> 25 // 3 6:dup 26 // 4 7:invokespecial #4 <Method void StringBuilder()> 27 // 5 10:ldc1 #5 <String "ok"> 28 // 6 12:invokevirtual #6 <Method StringBuilder StringBuilder.append(String)> 29 // 7 15:aload_1 30 // 8 16:invokevirtual #6 <Method StringBuilder StringBuilder.append(String)> 31 // 9 19:ldc1 #7 <String "xyz"> 32 // 10 21:invokevirtual #6 <Method StringBuilder StringBuilder.append(String)> 33 // 11 24:iconst_5 34 // 12 25:invokevirtual #8 <Method StringBuilder StringBuilder.append(int)> 35 // 13 28:invokevirtual #9 <Method String StringBuilder.toString()> 36 // 14 31:astore_2 37 System.out.println(ss); 38 // 15 32:getstatic #10 <Field PrintStream System.out> 39 // 16 35:aload_2 40 // 17 36:invokevirtual #11 <Method void PrintStream.println(String)> 41 // 18 39:return 42 } 43 }
1 public class test2 { 2 public static void main(String[] args) { 3 String s = ""; 4 Random rand = new Random(); 5 for (int i = 0; i < 10; i++) { 6 s = s + rand.nextInt(1000) + " "; 7 } 8 System.out.println(s); 9 } 10 }
用反编译工具jad执行jad -o -a -s d.java test2.class进行反编译后,发现其内部同样是通过StringBuilder来进行拼接的:
1 // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. 2 // Jad home page: http://www.kpdus.com/jad.html 3 // Decompiler options: packimports(3) annotate 4 // Source File Name: test2.java 5 package example; 6 import java.io.PrintStream; 7 import java.util.Random; 8 public class test2 9 { 10 public test2() 11 { 12 // 0 0:aload_0 13 // 1 1:invokespecial #1 <Method void Object()> 14 // 2 4:return 15 } 16 public static void main(String args[]) 17 { 18 String s = ""; 19 // 0 0:ldc1 #2 <String ""> 20 // 1 2:astore_1 21 Random rand = new Random(); 22 // 2 3:new #3 <Class Random> 23 // 3 6:dup 24 // 4 7:invokespecial #4 <Method void Random()> 25 // 5 10:astore_2 26 for(int i = 0; i < 10; i++) 27 //* 6 11:iconst_0 28 //* 7 12:istore_3 29 //* 8 13:iload_3 30 //* 9 14:bipush 10 31 //* 10 16:icmpge 55 32 s = (new StringBuilder()).append(s).append(rand.nextInt(1000)).append(" ").toString(); 33 // 11 19:new #5 <Class StringBuilder> 34 // 12 22:dup 35 // 13 23:invokespecial #6 <Method void StringBuilder()> 36 // 14 26:aload_1 37 // 15 27:invokevirtual #7 <Method StringBuilder StringBuilder.append(String)> 38 // 16 30:aload_2 39 // 17 31:sipush 1000 40 // 18 34:invokevirtual #8 <Method int Random.nextInt(int)> 41 // 19 37:invokevirtual #9 <Method StringBuilder StringBuilder.append(int)> 42 // 20 40:ldc1 #10 <String " "> 43 // 21 42:invokevirtual #7 <Method StringBuilder StringBuilder.append(String)> 44 // 22 45:invokevirtual #11 <Method String StringBuilder.toString()> 45 // 23 48:astore_1 46 47 // 24 49:iinc 3 1 48 //* 25 52:goto 13 49 System.out.println(s); 50 // 26 55:getstatic #12 <Field PrintStream System.out> 51 // 27 58:aload_1 52 // 28 59:invokevirtual #13 <Method void PrintStream.println(String)> 53 // 29 62:return 54 } 55 }
1 public class test4 { 2 public static void main(String[] args) { 3 final int MAX=10000000; 4 System.out.println("不用intern:"+notIntern(MAX)); 5 // System.out.println("使用intern:"+intern(MAX)); 6 } 7 private static long notIntern(int MAX){ 8 long start = System.currentTimeMillis(); 9 for (int i = 0; i < MAX; i++) { 10 int j = i % 100; 11 String str = String.valueOf(j); 12 } 13 return System.currentTimeMillis() - start; 14 } 15 /* 16 private static long intern(int MAX){ 17 long start = System.currentTimeMillis(); 18 for (int i = 0; i < MAX; i++) { 19 int j = i % 100; 20 String str = String.valueOf(j).intern(); 21 } 22 return System.currentTimeMillis() - start; 23 }*/ 24
未使用intern的GC日志:
1 不用intern:354 2 [GC (System.gc()) [PSYoungGen: 377487K->760K(2752512K)] 377487K->768K(2758656K), 0.0009102 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 3 [Full GC (System.gc()) [PSYoungGen: 760K->0K(2752512K)] [ParOldGen: 8K->636K(6144K)] 768K->636K(2758656K), [Metaspace: 3278K->3278K(1056768K)], 0.0051214 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 4 Heap 5 PSYoungGen total 2752512K, used 23593K [0x0000000700000000, 0x00000007c0000000, 0x00000007c0000000) 6 eden space 2359296K, 1% used [0x0000000700000000,0x000000070170a548,0x0000000790000000) 7 from space 393216K, 0% used [0x0000000790000000,0x0000000790000000,0x00000007a8000000) 8 to space 393216K, 0% used [0x00000007a8000000,0x00000007a8000000,0x00000007c0000000) 9 ParOldGen total 6144K, used 636K [0x0000000640000000, 0x0000000640600000, 0x0000000700000000) 10 object space 6144K, 10% used [0x0000000640000000,0x000000064009f2f8,0x0000000640600000) 11 Metaspace used 3284K, capacity 4500K, committed 4864K, reserved 1056768K 12 class space used 359K, capacity 388K, committed 512K, reserved 1048576K
根据打印的日志分析:没有使用intern情况下,执行时间为354ms,占用内存为24229k;
1 使用intern:1515 2 [GC (System.gc()) [PSYoungGen: 613417K->1144K(2752512K)] 613417K->1152K(2758656K), 0.0012530 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 3 [Full GC (System.gc()) [PSYoungGen: 1144K->0K(2752512K)] [ParOldGen: 8K->965K(6144K)] 1152K->965K(2758656K), [Metaspace: 3780K->3780K(1056768K)], 0.0079962 secs] [Times: user=0.02 sys=0.00, real=0.01 secs] 4 Heap 5 PSYoungGen total 2752512K, used 15729K [0x0000000700000000, 0x00000007c0000000, 0x00000007c0000000) 6 eden space 2359296K, 0% used [0x0000000700000000,0x0000000700f5c400,0x0000000790000000) 7 from space 393216K, 0% used [0x0000000790000000,0x0000000790000000,0x00000007a8000000) 8 to space 393216K, 0% used [0x00000007a8000000,0x00000007a8000000,0x00000007c0000000) 9 ParOldGen total 6144K, used 965K [0x0000000640000000, 0x0000000640600000, 0x0000000700000000) 10 object space 6144K, 15% used [0x0000000640000000,0x00000006400f1740,0x0000000640600000) 11 Metaspace used 3786K, capacity 4540K, committed 4864K, reserved 1056768K 12 class space used 420K, capacity 428K, committed 512K, reserved 1048576K
1 public class test5 { 2 public static void main(String[] args) { 3 4 String s1=new String("ab"); 5 s.intern(); 6 String s2="ab"; 7 System.out.println(s1==s2); 8 9 10 String s3=new String("ab")+new String("cd"); 11 s3.intern(); 12 String s4="abcd"; 13 System.out.println(s4==s3); 14 } 15 }