2020年9月18日 StringBuffer 和 StringBuilder 和 String 的 效率问题;

package com.atguigu.test10;

/*
 * Runtime:JVM运行时环境
 * Runtime是一个单例的实现
 */
public class TestTime {
    public static void main(String[] args) {
//        testStringBuilder();
        testStringBuffer();
//        testString();
    }
    public static void testString(){
        long start = System.currentTimeMillis();
        String s = new String("0");

            s += i;
        }
        long end = System.currentTimeMillis();
        System.out.println("String拼接+用时:"+(end-start));//444
        
        long memory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        System.out.println("String拼接+memory占用内存: " + memory);//53185144字节
    }
    public static void testStringBuilder(){
        long start = System.currentTimeMillis();
        StringBuilder s = new StringBuilder("0");
        for(int i=1;i<=10000;i++){
            s.append(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("StringBuilder拼接+用时:"+(end-start));//4
        long memory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        System.out.println("StringBuilder拼接+memory占用内存: " + memory);//1950488
    }
    public static void testStringBuffer(){
        long start = System.currentTimeMillis();
        StringBuffer s = new StringBuffer("0");
        for(int i=1;i<=10000;i++){
            s.append(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("StringBuffer拼接+用时:"+(end-start));//7
        long memory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
        System.out.println("StringBuffer拼接+memory占用内存: " + memory);//1950488
    }
}

 

posted @ 2020-09-18 14:25  窦云鹏  阅读(160)  评论(0编辑  收藏  举报