zoukankan      html  css  js  c++  java
  • java--字符串拼接比较

    /**
     * 字符串拼接
     */
    public class StringAddDemo {
        public static void main(String[] args){
            testStringAdd();
            testConcat();
            testStringBuilder();
            testStringBuffer();
            //+:676
            //concat:243
            //StringBuilder:9
            //StringBuffer:1
        }
        public static void testStringAdd(){
            String s = "";
            long begin = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                s = s + i;
            }
            long end = System.currentTimeMillis();
            System.out.println("+:" + (end-begin));
        }
        //连接
        public static void testConcat(){
            String s = "";
            long begin = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                s = s.concat(String.valueOf(i));
            }
            long end = System.currentTimeMillis();
            System.out.println("concat:" + (end-begin));
        }
        //synchronization
        public static void testStringBuffer(){
            StringBuffer sb = new StringBuffer();
            long begin = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                sb.append(i);
            }
            sb.toString();
            long end = System.currentTimeMillis();
            System.out.println("StringBuffer:" + (end-begin));
        }
        public static void testStringBuilder(){
            StringBuilder sb = new StringBuilder();
            long begin = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                sb.append(i);
            }
            sb.toString();
            long end = System.currentTimeMillis();
            System.out.println("StringBuilder:" + (end-begin));
        }
    }
    
  • 相关阅读:
    去掉安装程序被挂起,要重新启动电脑
    为什么要关闭数据库连接,可以不关闭吗?
    读取Excel异常定义了过多字段的解决方法
    关于打开ILDASM的方法
    SQL Server数据类型
    C++的MFC,与C#的.NET
    javascript数据类型
    日志记录组件[Log4net]详细介绍
    Xml的读取
    yield让代码更加简洁
  • 原文地址:https://www.cnblogs.com/fly-book/p/11340778.html
Copyright © 2011-2022 走看看