Home > Java > javaTutorial > body text

Java Example - String Performance Comparison Test

黄舟
Release: 2017-02-22 09:47:50
Original
1186 people have browsed it

The following example demonstrates creating a string in two ways and testing its performance:

//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)
      + " 毫秒");
   }}
Copy after login

The output result of the above code example is:

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

The above is the content of Java Example-String Performance Comparison Test. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!




##
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template