首页 > 编程语言 > 详细

Java 实例 – 字符串性能比较测试

时间:2018-03-03 14:58:55      阅读:188      评论:0      收藏:0      [点我收藏+]
package string;

public class StringComparePerformance {

    public static void main(String[] args) {
        /**
         * 字符串性能测试比较
         */
        Long startTime = System.currentTimeMillis();
        for (int i = 0; i < 500000000; i++) {
            String str1 = "朱小胖";
            String str2 = "朱小胖";
        }
        Long endTime = System.currentTimeMillis();
        System.out
                .println("使用String关键词创建字符串时间:" + (endTime - startTime) + "毫秒");
        Long startTime1 = System.currentTimeMillis();
        for (int i = 0; i < 500000000; i++) {
            String str1 = new String("朱胖胖");
            String str2 = new String("朱胖胖");
        }
        Long endTime1 = System.currentTimeMillis();
        System.out.println("使用String对象创建字符串时间:" + (endTime1 - startTime1)
                + "毫秒");
    }

}

测试结果

使用String关键词创建字符串时间:4毫秒
使用String对象创建字符串时间:30毫秒

 

Java 实例 – 字符串性能比较测试

原文:https://www.cnblogs.com/zhuxiaopang/p/8496519.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!