• 选择合适的String拼接方法(这篇博客是我抄的)


    package com.test;
    
    public class FreeFile {
    
        public static void main(String[] args) {
            // 加号拼接
            String str = "";
            long start1 = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                str += "c";
            }
            long end1 = System.currentTimeMillis();
            System.out.println("加号拼接耗时:" + (end1 - start1) + "ms");
    
            // concat拼接
            str = "";
            long start2 = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                str = str.concat("c");
            }
            long end2 = System.currentTimeMillis();
            System.out.println("concat拼接耗时:" + (end2 - start2) + "ms");
    
            // StringBuilder拼接
            str = "";
            StringBuilder buffer = new StringBuilder("");
            long start3 = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                buffer.append("c");
            }
            long end3 = System.currentTimeMillis();
            System.out.println("StringBuilder拼接耗时:" + (end3 - start3) + "ms");
    
            // StringBuffer拼接
            str = "";
            StringBuffer sb = new StringBuffer("");
            long start4 = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                sb.append("c");
            }
            long end4 = System.currentTimeMillis();
            System.out.println("StringBuffer拼接耗时:" + (end4 - start4) + "ms");
    
        }
    
    }

  • 相关阅读:
    event 事件 键盘控制div移动
    event 事件 div鼠标跟随
    获取坐标封装 getPos
    event 事件 clientX 和clientY 配合scrollTop使用, div跟着鼠标走
    event 事件 冒泡
    event 事件 坐标兼容
    event事件基础 document
    DOM 多字符搜索
    DOM search table 模糊搜索
    Reverse a sentence
  • 原文地址:https://www.cnblogs.com/wgbs25673578/p/5882210.html
Copyright © 2020-2023  润新知