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://codercto.com/courses/l/12.html

查看所有标签

Coding the Matrix

Coding the Matrix

Philip N. Klein / Newtonian Press / 2013-7-26 / $35.00

An engaging introduction to vectors and matrices and the algorithms that operate on them, intended for the student who knows how to program. Mathematical concepts and computational problems are motiva......一起来看看 《Coding the Matrix》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具