關於ArrayList,FastArrayList,TreeList,Vector,Stack的效能測試

来源:互联网 发布:数据恢复软件代理 编辑:程序博客网 时间:2024/06/12 22:30
package test.list;import static java.lang.System.out;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Stack;import java.util.Vector;import java.util.concurrent.CopyOnWriteArrayList;import org.apache.commons.collections.FastArrayList;import org.apache.commons.collections.list.TreeList;import org.apache.commons.lang3.StringUtils;import org.apache.commons.lang3.time.StopWatch;@SuppressWarnings("unchecked")public class ListPerformance {public static void main(String[] args) {ListPerformance test = new ListPerformance(10*10000);out.print(StringUtils.center("Test List Performance: loop=" + test.loop, 80, '-'));out.println("\n\t\tadd\tinsert\tremove\tget\tset\titerator\tfor");test.benchmark(new FastArrayList());test.benchmark(new TreeList());test.benchmark(new ArrayList());test.benchmark(new LinkedList());test.benchmark(new CopyOnWriteArrayList());test.benchmark(new Vector());test.benchmark(new Stack());// 2.测试排序out.print("\n\n");out.print(StringUtils.center("Test List sort Performance: loop="+ test.loop, 80, '-'));out.printf("\n\t\t\toptimize\tworst\trandom");test.benchmarkSort(new FastArrayList());test.benchmarkSort(new TreeList());test.benchmarkSort(new ArrayList());test.benchmarkSort(new LinkedList()); test.benchmarkSort(new CopyOnWriteArrayList());//UnsupportedOperationExceptiontest.benchmarkSort(new Vector());test.benchmarkSort(new Stack());// 3.测试各种数据结构间转化out.print("\n\n");out.print(StringUtils.center("Test List convert Performance: loop="+ test.loop, 80, '-'));out.printf("\n\t\tTree\tArray\tLinked\tCopyOnWrite\tVector");test.benchmarkConvert(new FastArrayList());test.benchmarkConvert(new TreeList());test.benchmarkConvert(new ArrayList());test.benchmarkConvert(new LinkedList());//test.benchmarkConvert(new CopyOnWriteArrayList());}private int loop = 10000;public ListPerformance(int loop) {this.loop = loop;}public void benchmark(List list) {out.format(StringUtils.left("\n" + list.getClass().getSimpleName(), 15));int j;StopWatch watch = null;// 1.测试顺序性能(Add)(watch = new StopWatch()).start();for (int i = 0; i < loop; i++) {list.add(new Integer(i));}watch.stop();out.printf("\t" + watch.getTime());// 2.测试随机插入性能(Random insert)(watch = new StopWatch()).start();for (int i = 0; i < loop; i++) {j = (int) (Math.random() * loop);list.add(j, new Integer(-j));}watch.stop();out.printf("\t" + watch.getTime());// 3.测试随机索引删除(Random remove)(watch = new StopWatch()).start();for (int i = 0; i < loop; i++) {j = (int) (Math.random() * loop);list.remove(j);}watch.stop();out.printf("\t" + watch.getTime());// 4.测试随机取数性能(Random get)(watch = new StopWatch()).start();for (int i = 0; i < loop; i++) {j = (int) (Math.random() * loop);list.get(j);}watch.stop();out.printf("\t" + watch.getTime());// 5.测试随机更新性能(Random set)(watch = new StopWatch()).start();for (int i = 0; i < loop; i++) {j = (int) (Math.random() * loop);list.set(j, j);}watch.stop();out.printf("\t" + watch.getTime());// 6.测试迭代性能(Iterator)(watch = new StopWatch()).start();Iterator<Object> iter = list.iterator();while (iter.hasNext()) {iter.next();}watch.stop();out.printf("\t" + watch.getTime());// 7.测试迭代性能(Iterator)(watch = new StopWatch()).start();// Iterator<Object> iter = list.iterator();for (Object obj : list) {}watch.stop();out.printf("\t" + watch.getTime());}public void benchmarkConvert(List list) {out.format(StringUtils.left("\n" + list.getClass().getSimpleName(), 15));StopWatch watch = null;// 1.转TreeList(watch = new StopWatch()).start();new TreeList(list);watch.stop();out.printf("\t" + watch.getTime());// 2.转ArrayList(watch = new StopWatch()).start();new ArrayList(list);watch.stop();out.printf("\t" + watch.getTime());// 3.转LinkedList(watch = new StopWatch()).start();new LinkedList(list);watch.stop();out.printf("\t" + watch.getTime());// 4.转CopyOnWriteArrayList(watch = new StopWatch()).start();new CopyOnWriteArrayList(list);watch.stop();out.printf("\t" + watch.getTime());// 5.转Vector(watch = new StopWatch()).start();new Vector(list);watch.stop();out.printf("\t" + watch.getTime());}public void benchmarkSort(List list) {out.format(StringUtils.left("\n" + list.getClass().getSimpleName(), 15));StopWatch watch = null;// 1.顺序Listfor (int i = 0; i < loop; i++) {list.add(new Integer(i));}(watch = new StopWatch()).start();Collections.sort(list);watch.stop();out.printf("\t" + watch.getTime());// 2.逆序Listfor (int i = loop - 1; i > 0; i--) {list.add(new Integer(i));}(watch = new StopWatch()).start();Collections.sort(list);watch.stop();out.printf("\t" + watch.getTime());// 3.随机顺序Listfor (int i = 0, j = 0; i < loop; i++) {j = (int) (Math.random() * loop);list.add(new Integer(j));}(watch = new StopWatch()).start();Collections.sort(list);watch.stop();out.printf("\t" + watch.getTime());}}

關於其中list 的測試結果。如下:(其中,測試數據量爲:=100000)

ClassaddInsertremovegetsetiteratorforFastArrayList147356739181743TreeList77154123394163ArrayList207275726461833LinkedList41109875164135943436729921CopyOnWriteArr8049586816931074458121Vector886232030263022Stack2722773726622,以上表明:

在Iterator,和for的效能,各種list 是沒差的。並且很迅速。

單純的ArrayList 在add,get,set 就夠用,效能也算不錯。

TreeList 在add,insert,romove,get,set 上都有不俗的表現,其取決於其數據結構。

LinkedList ,由於數據結構爲鏈表形式,其數據結構限制了LinkedList 的查找效率,造成get()無效率,insert,romove 都需要用到查找方法,故結果這麼慢。

Vector 基於線程安全的,不做解釋。

FastArrayList 和ArrayList ,個人覺得没查。





原创粉丝点击