zoukankan      html  css  js  c++  java
  • 五种拼字条串方法

    引用:http://www.cnblogs.com/focusj/archive/2011/02/16/2057630.html

    package test;
    import org.apache.commons.lang.StringUtils;
    public class CutString {
        //性能最差
        public String testPlus() {
            String s = "";
            for (int i = 0; i < 10000; i++) {
                s = s + String.valueOf(i);
            }
            return s;
        }
        //较好
        public String testConcat() {
            String s = "";
            for (int i = 0; i < 10000; i++) {
                s = s.concat(String.valueOf(i));
            }
            return s;
        }
         
        //性能与testStringBuffer相差无几,比其要好
        public String testJoin() {
            String[] list = new String[10000];
            for (int i = 0; i < 10000; i++) {
                list[i] = String.valueOf(i);
            }
            return StringUtils.join(list, "");
        }
        public String testStringBuffer() {
            StringBuffer sb = new StringBuffer();
            long l1 = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                sb.append(String.valueOf(i));
            }
            long l2 = System.currentTimeMillis();
            System.out.println(l2-l1);
            return sb.toString();
        }
         
        //性能最高的方法
        public void testStringBuilder() {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 100000; i++) {
                sb.append(String.valueOf(i));
            }
            sb.toString();
        }
        public static void main(String[] args) {
            System.out.println(new CutString().testStringBuffer());
        }
    }
  • 相关阅读:
    Yougth的最大化(好题,二分查找 0 1分数规划)
    Cable master(好题,二分)
    Can you find it?(二分 二分+STL set map)
    Can you solve this equation?(二分)
    Bridging signals(二分 二分+stl dp)
    A Bug's Life
    Is It A Tree?(并查集)
    简单计算器(栈)
    Linux学习之常用压缩命令(三)
    Linux系统之常用文件搜索命令
  • 原文地址:https://www.cnblogs.com/sode/p/2469925.html
Copyright © 2011-2022 走看看