时间:2021-07-01 10:21:17 帮助过:40人阅读
1 public class Test { 2 public static void main(String args[]) { 3 4 String str = "abc"; 5 StringBuffer sb = new StringBuffer("abc"); 6 Runtime runtime = Runtime.getRuntime(); 7 long start = System.currentTimeMillis(); 8 long startFreememory = runtime.freeMemory(); 9 for (int i = 0; i < 10000; i++) { 10 str += i; 11 //测试StringBuffer时候把注释打开 12 //sb.append(i); 13 } 14 long endFreememory = runtime.freeMemory(); 15 long end = System.currentTimeMillis(); 16 System.out.println("操作耗时:" + (end - start) + "ms," + "内存消耗:" 17 + (startFreememory - endFreememory)/1024 + "KB"); 18 } 19 }
测试结果:
使用String做10000次向一字符串后添加字符串
操作耗时:1872ms,内存消耗:1301KB
使用StringBuffer做10000次向一字符串后添加字符串
操作耗时:15ms,内存消耗:162KB
差别显著!
Dao持久层定义sql语句拼接参数之String和StringBuffer区别
标签:bsp 使用 lte build list blog 复制 的区别 template