Java Performance when you go through an entire list of elements

This week I was told that my way of going through each elements in an ArrayList was not the most efficient. I should be using an Iterator.

No reason to doubt an experienced programmer so I changed my code.

I never trusted anyone when I did troubleshooting before. I always need to see the evidence for myself. So I decided to do this code to check the validity of the data I was given:

/**
 * Testing the performance of different ways to go through a list of items
 */
package com.cinq.test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * @author mc
 * @version 0.1.0
 * 
 * last updated on 2011-11-27
 *  initial release of these tests
 */
public class Performance {

    /**
     * @param args
     */
    public static void main(String[] args) {

        long dstart = 0;
        long dend = 0;

        // Allocate an ArrayList
        ArrayList<String> data1 = new ArrayList<String>();
        ArrayList<String> data2 = new ArrayList<String>();
        for ( int i = 0; i < 10000; i++ ) {
            data1.add("abc" + String.valueOf(i));
            data2.add("xyz" + String.valueOf(i));
        }

        // Start going through all the elements with a get index method
        dstart = System.currentTimeMillis();
        for ( int j = 0; j < data1.size(); j++ ) {
            data1.get(j);
            for ( int k = 0 ; k < data2.size(); k++) {
                data2.get(k);
            }
        }
        dend = System.currentTimeMillis();

        System.out.println("Get Index for " + data1.size() + " by " + data2.size() + " elements took " + (dend - dstart) + " ms.");

        dstart = System.currentTimeMillis();
        for ( String s1 : data1 ) {
            for ( String s2 : data2 ) {
                // nothing
            }
        }
        dend = System.currentTimeMillis();

        System.out.println("For each for " + data1.size() + " by " + data2.size() + " elements took " + (dend - dstart) + " ms.");

        dstart = System.currentTimeMillis();
        Iterator<String> it1 = data1.iterator();
        try {
            while ( it1.next() != null ) {
                try {
                    Iterator<String> it2 = data2.iterator();
                    while ( it2.next() != null ) {
                        //nothing
                    }
                } catch ( NoSuchElementException e0 ) {
                    //ignore
                }
            }
        } catch ( NoSuchElementException e1 ) {
            // ignore
        }
        dend = System.currentTimeMillis();

        System.out.println("Iterator for " + data1.size() + " by " + data2.size() + " elements took " + (dend - dstart) + " ms.");
    }

}

The output I get from this is:

Get Index for 10000 by 10000 elements took 12 ms.
For each for 10000 by 10000 elements took 271 ms.
Iterator for 10000 by 10000 elements took 166 ms.

This makes no sense. I should see the performance of the Iterator way of doing things to be faster than my get(index) way. I will have to ask my co-worker tomorrow why I am getting this bad performance. Conclusion to follow in the next few days.

Advertisements

Published by

m5c

Java developper that loves photography and good coffee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s