zoukankan      html  css  js  c++  java
  • 持有对象---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的要快更多

  • 相关阅读:
    Spring IoC容器实现
    Spring IoC简介及使用
    tomcat使用及原理
    tomcat的连接数与线程池
    tomcat配置文件server.xml
    java源码之Comparable和Comparator
    java源码之TreeSet
    25 二叉搜索树与双向链表
    24 复杂链表的复制
    条款04:确定对象被使用前已被初始化
  • 原文地址:https://www.cnblogs.com/alagong/p/10274712.html
Copyright © 2011-2022 走看看