Home  >  Article  >  Java  >  Java Example - String Performance Comparison Test

Java Example - String Performance Comparison Test

黄舟
黄舟Original
2017-02-22 09:47:501190browse

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)
      + " 毫秒");
   }}

The output result of the above code example is:

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

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)!




##
Statement:
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