zoukankan      html  css  js  c++  java
  • Java 实例

    以下实例演示了通过 "+" 操作符和StringBuffer.append() 方法来连接字符串,并比较其性能:

    StringConcatenate.java 文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    public class StringConcatenate {
        public static void main(String[] args){
            long startTime = System.currentTimeMillis();
            for(int i=0;i<5000;i++){
                String result = "This is"
                "testing the"
                "difference""between"
                "String""and""StringBuffer";
            }
            long endTime = System.currentTimeMillis();
            System.out.println("字符串连接" 
            " - 使用 + 操作符 : " 
            + (endTime - startTime)+ " ms");
            long startTime1 = System.currentTimeMillis();
            for(int i=0;i<5000;i++){
                StringBuffer result = new StringBuffer();
                result.append("This is");
                result.append("testing the");
                result.append("difference");
                result.append("between");
                result.append("String");
                result.append("and");
                result.append("StringBuffer");
            }
            long endTime1 = System.currentTimeMillis();
            System.out.println("字符串连接" 
            " - 使用 StringBuffer : "
            + (endTime1 - startTime1)+ " ms");
        }
    }

    以上代码实例输出结果为:

    点击链接查看结果

  • 相关阅读:
    Day-11 闭包和迭代器
    Day-01 Python基础
    Day-10 函数的进阶
    Day-09 初识函数
    Day-08 文件操作
    Day-07 基础数据类型补充 set集合 深浅拷贝
    Day-06 小数据池 再谈编码
    Day-05 基础数据类型字典dict
    Day-04 基础数据类型list, tuple
    NodeJs获取两个日期间的所有日期
  • 原文地址:https://www.cnblogs.com/hane/p/7325617.html
Copyright © 2011-2022 走看看