代码如下 | 复制代码 |
package com.letv.cloud.cdn.jtest; import java.io.IOException; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.concurrent.TimeUnit; import org.slf4j.LoggerFactory; import com.google.common.base.Stopwatch; public class JtestMain { private final static org.slf4j.Logger LOGGER = LoggerFactory.getLogger("JtestMain"); private static Stopwatch stopWatch = new Stopwatch(); public static void testList(int num){ List<String> aList = new ArrayList<String>(); List<String> lList = new LinkedList<String>(); stopWatch.reset(); stopWatch.start(); for(int i=0; i<num; i++){ aList.add("201411180827 cctv5_800 83 796.03 115.182.51.134 123.125.89.75 in uuid1 5"); } LOGGER.info("insert num:{} ArrayList cost:{} ms",num,stopWatch.elapsedTime(TimeUnit.MILLISECONDS)); stopWatch.reset(); stopWatch.start(); for(int i=0; i<num; i++){ lList.add("201411180827 cctv5_800 83 796.03 115.182.51.134 123.125.89.75 in uuid1 5"); } LOGGER.info("insert num:{} LinkedList cost:{} ms",num,stopWatch.elapsedTime(TimeUnit.MILLISECONDS)); } public static void main(String[] args) throws InterruptedException, IOException { int[] nums = {10000,10 * 10000,100 * 10000, 1000 * 10000, 10000 * 10000}; for(int i=0; i<nums.length; i++){ testList(nums[i]); } } } |
测试结果:
代码如下 | 复制代码 |
INFO [main] (JtestMain.java:65) 11:50:47,480 -- insert num:10000 ArrayList cost:2 ms INFO [main] (JtestMain.java:72) 11:50:47,484 -- insert num:10000 LinkedList cost:1 ms INFO [main] (JtestMain.java:65) 11:50:47,490 -- insert num:100000 ArrayList cost:5 ms INFO [main] (JtestMain.java:72) 11:50:47,495 -- insert num:100000 LinkedList cost:5 ms INFO [main] (JtestMain.java:65) 11:50:47,504 -- insert num:1000000 ArrayList cost:8 ms INFO [main] (JtestMain.java:72) 11:50:47,515 -- insert num:1000000 LinkedList cost:11 ms INFO [main] (JtestMain.java:65) 11:50:47,606 -- insert num:10000000 ArrayList cost:89 ms INFO [main] (JtestMain.java:72) 11:50:49,335 -- insert num:10000000 LinkedList cost:1729 ms INFO [main] (JtestMain.java:65) 11:50:50,290 -- insert num:100000000 ArrayList cost:954 ms INFO [main] (JtestMain.java:72) 11:51:14,263 -- insert num:100000000 LinkedList cost:23973 ms |
从上面的测试数据我们可以看得到ArrayList 是LinkedList几倍了,数据越大它们的区别就越能体现出来了。
时间: 2024-09-28 03:12:31