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

Java 教程 · 2019-02-10 08:58:32

以下实例演示了通过两种方式创建字符串,并测试其性能:

StringComparePerformance.java 文件

public class StringComparePerformance{ public static void main(String[] args){ long startTime = System.currentTimeMillis(); for(int i=0;i<50000;i++){ String s1 = "hello"; String s2 = "hello"; } long endTime = System.currentTimeMillis(); System.out.println("通过 String 关键词创建字符串" + " : "+ (endTime - startTime) + " 毫秒" ); long startTime1 = System.currentTimeMillis(); for(int i=0;i<50000;i++){ String s3 = new String("hello"); String s4 = new String("hello"); } long endTime1 = System.currentTimeMillis(); System.out.println("通过 String 对象创建字符串" + " : " + (endTime1 - startTime1) + " 毫秒"); } }

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

通过 String 关键词创建字符串 : 6 毫秒 
通过 String 对象创建字符串 : 14 毫秒

点击查看所有 Java 教程 文章: https://www.codercto.com/courses/l/12.html

查看所有标签

High Performance Python

High Performance Python

Andrew Lewis / O'Reilly Media, Inc. / 2010-09-15 / USD 34.99

Chapter 1. Introduction Section 1.1. The High Performance Buzz-word Chapter 2. The Theory of Computation Section 2.1. Introduction Section 2.2. Problems Section 2.3. Models of Computati......一起来看看 《High Performance Python》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码