String,StringBuilder,StringBuffer時間比較
阿新 • • 發佈:2019-05-08
pro pri public code tar rgs end art std
String,StringBuilder,StringBuffer時間比較
1 package com.cst.iprocess.controller; 2 import java.util.Date; 3 4 public class test { 5 6 public static void main(String[] args) { 7 //String,StringBuilder,StringBuffer時間比較 8 //String 9 String str = "";10 long starTime = System.currentTimeMillis(); 11 for(int i = 0;i<10000;i++) { 12 str=str+i; 13 } 14 long endTime = System.currentTimeMillis(); 15 long time = endTime - starTime; 16 System.out.println("String的執行時間為:"+time); 17 18//StringBuilder 19 StringBuilder std = new StringBuilder(); 20 starTime = System.currentTimeMillis(); 21 for(int i = 0;i<10000;i++) { 22 std.append(i); 23 } 24 endTime = System.currentTimeMillis(); 25 time = endTime - starTime;26 System.out.println("StringBuilder的執行時間為:"+time); 27 28 //StringBuffer 29 StringBuffer stb = new StringBuffer(); 30 starTime = System.currentTimeMillis(); 31 for(int i = 0;i<10000;i++) { 32 stb.append(i); 33 } 34 endTime = System.currentTimeMillis(); 35 time = endTime - starTime; 36 System.out.println("StringBuffer的執行時間為:"+time); 37 } 38 } 39
執行時間
1 String的執行時間為:145
2 StringBuilder的執行時間為:1
3 StringBuffer的執行時間為:0
String,StringBuilder,StringBuffer時間比較