zoukankan      html  css  js  c++  java
  • 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
        }
    }
  • 相关阅读:
    GridView跨列
    html的积累
    什么是json?
    关于string
    Effective C# Item38:定制和支持数据绑定
    Effective C# Item44:为应用程序创建特定的异常类
    Effective C# Item42:利用特性简化反射
    Effective C# Item47:选择安全代码
    Effective C# Item43 : 避免过度使用反射
    Effective C# Item39 : 使用.NET验证
  • 原文地址:https://www.cnblogs.com/douyunpeng/p/13691054.html
Copyright © 2011-2022 走看看