持有对象---Arrays.asList()和Collections.addAll()的性能比较

    • import java.util.ArrayList; 
      import java.util.Arrays; 
      import java.util.Collections; 
      import java.util.List; 
      public class ArraysCompareCollectionsTest { 
      public static void main(String[] args) { 
      int count = 10000000;//通过改变循环次数来放大或放小结果 
      System.out.println("Arrays's time is " + arraysTest(count)); 
      System.out.println("Collections's time is " + collectionsTest(count)); 

      public static long arraysTest(int count) { 
      long startTime = System.currentTimeMillis(); 
      for (int i = 0; i < count; i++) { 
      List arrays = new ArrayList (); 
      arrays.addAll(Arrays.asList("hello", "java", "world")); 

      return System.currentTimeMillis() - startTime; 
      }public static long collectionsTest(int count) { 
      long startTime = System.currentTimeMillis(); 
      for (int i = 0; i < count; i++) { 
      List arrays = new ArrayList (); 
      Collections.addAll(arrays,"hello", "java", "world"); 

      return System.currentTimeMillis() - startTime; 


      二、控制台输出结果分析


      1.当count=100000时,Collections的运行速度比Arrays的要快60ms左右


      2.当count=1000000时,Collections的运行速度比Arrays的要快100ms左右


      3.当count=10000000时,Collections的运行速度比Arrays的要快更多

    •  

posted @ 2019-01-15 22:39  阿拉贡二世  阅读(1086)  评论(0编辑  收藏  举报