for iterator ArrayList遍歴効率テスト

4694 ワード

/**
 *    ArrayList iterator for 
 *  :    LinkedList
 *  
 */
public class IteratorTest {

    static int size = 1000000;
    static List<Object> list1 = new ArrayList<Object>();
    static List<Object> list2 = new ArrayList<Object>();
    static {
        for (int i = 0; i < size; i++) {
            list1.add(new Object());
            list2.add(new Object());
        }
    }

    @Test
    public void iteratorTest() {
        long start = System.currentTimeMillis();
        for (Iterator<Object> iterator = list1.iterator(); iterator.hasNext();) {
            Object entry = (Object) iterator.next();
            // System.out.print(entry);
        }
        System.out.println("
iterator " + (System.currentTimeMillis() - start) + " ms"); } @Test(dependsOnMethods = "iteratorTest") public void forTest() { long start = System.currentTimeMillis(); for (int i = 0, size = list2.size(); i < size; i++) {// size() , list , Object entry = list2.get(i); // System.out.print(entry); } System.out.println("
for " + (System.currentTimeMillis() - start) + " ms"); } @Test(groups="alone",invocationCount=10) //testng 10 public void compareTest() { long start = System.currentTimeMillis(); for (Iterator<Object> iterator = list1.iterator(); iterator.hasNext();) { Object entry = (Object) iterator.next(); // System.out.print(entry); } long time=System.currentTimeMillis()-start; start = System.currentTimeMillis(); for (int i = 0, size = list2.size(); i < size; i++) { Object entry = list2.get(i); // System.out.print(entry); } long time2=System.currentTimeMillis() - start; System.out.println("iterator "+time +" ms, for "+time2+" ms"); } }

 
しゅつりょく
iterator 32 ms

for 15 ms
iterator 32 ms, for 15 ms
iterator 31 ms, for 0 ms
iterator 31 ms, for 0 ms
iterator 32 ms, for 15 ms
iterator 31 ms, for 16 ms
iterator 31 ms, for 16 ms
iterator 31 ms, for 0 ms
iterator 31 ms, for 0 ms
iterator 31 ms, for 16 ms
iterator 31 ms, for 16 ms

 
 
遍歴中のremoveテスト
 @Test
    public void iteratorTest() {
        long start = System.currentTimeMillis();
        for (Iterator<Object> iterator = list1.iterator(); iterator.hasNext();) {
            Object entry = (Object) iterator.next();
            System.out.println(entry);
            iterator.remove();//right
        }
        System.out.println("
iterator " + (System.currentTimeMillis() - start) + " ms"); } @Test(expectedExceptions = IndexOutOfBoundsException.class) public void forTest() { long start = System.currentTimeMillis(); for (int i = 0, size = list2.size(); i < size; i++) { Object entry = list2.get(i); System.out.println(entry); list2.remove(i);//wrong } System.out.println("
for " + (System.currentTimeMillis() - start) + " ms"); }

 
しゅつりょく
0
1
2
3
4
5
6
7
8
9

iterator 0 ms
0
2
4
6
8

 
 
 
public static void main(String[] args) {
		List a =new ArrayList();
		for (int i = 0; i < 10; i++) {
			a.add(i);
			System.out.println(i);
		}
		System.out.println("-------------");
		int j =0;
		for (Iterator iterator = a.iterator(); iterator.hasNext();) {
			Object object = (Object) iterator.next();
			if(j%2==0){
				iterator.remove();
			}
			j++;
		}
		for (Object object : a) {
			System.out.println(object);
		}
	}



0
1
2
3
4
5
6
7
8
9
-------------
1
3
5
7
9