解答例 - 実習課題1 - 2.ListとIterator
(実習課題1)
「ArrayList」と「LinkedList」の性能を比較するプログラムを作成しなさい。
- あらかじめ100万個の要素をリストに追加しておく。
- それに対して「ランダムアクセス」「検索」「追加」「挿入」「削除」メソッドの実行に掛かる時間を計測し、表示する事。
- (ヒント)時間の計測には「System」クラスの「currentTimeMillis」メソッドを使用する。
解答例
package com.techscore.utility.chapter2.exercise1; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Random; /** * ListExample.java * TECHSCORE Javaユーティリティ2章 実習課題1 * * Copyright (c) 2004 Four-Dimensional Data, Inc. */ public class ListExample { public static void measureProcessTime(List list) { //ランダムアクセス Random random = new Random(); long before = System.currentTimeMillis(); list.get(random.nextInt(1000000)); long after = System.currentTimeMillis(); long time = after - before; System.out.println("Complete Random Access"); System.out.println("Time:" + time + " ms"); //検索 before = System.currentTimeMillis(); list.indexOf(new Integer(6875764)); after = System.currentTimeMillis(); time = after - before; System.out.println("Complete Find"); System.out.println("Time:" + time + " ms"); //追加 before = System.currentTimeMillis(); list.add(new Integer(1000000)); after = System.currentTimeMillis(); time = after - before; System.out.println("Complete Adding"); System.out.println("Time:" + time + " ms"); //挿入 before = System.currentTimeMillis(); list.add(1, new Integer(1111111)); after = System.currentTimeMillis(); time = after - before; System.out.println("Complete Insert"); System.out.println("Time:" + time + " ms"); //削除 before = System.currentTimeMillis(); list.remove(1); after = System.currentTimeMillis(); time = after - before; System.out.println("Complete Delete"); System.out.println("Time:" + time + " ms"); } public static void main(String args[]) { System.out.println("----Array List ------"); List arrayList = new ArrayList(); for (int i = 0; i < 1000000; i++) { arrayList.add(new Integer(i)); } ListExample.measureProcessTime(arrayList); System.out.println("---- Linked List ------"); List linkedList = new LinkedList(); for (int i = 0; i < 1000000; i++) { linkedList.add(new Integer(i)); } ListExample.measureProcessTime(linkedList); } }