开发者

comparing arraylist and linkedlist...is my code wrong?

I asked in another question why arraylist seemed faster than linkedlist when reading a file and to create the lists. I've now tried adding to the front of the list or the back of the list. Arraylist was still faster.

i just want to make sure i'm using these things right. here's what i'm doing:

public class LinkedListTest {

    private List<String> Names;

    public LinkedListTest(){
            Names = new LinkedList<String>();
    }

Then I just using linkedlist methods ie "Names.add(strings)". And when I tested arraylists, it's nearly identical:

public class ArrayListTest {

    private List<String> Names;

    public ArrayListTest(){
            Names = new ArrayList<String>();
    }

Am I doing it right? In fact, changing the list type in the const开发者_如何学Pythonructor method from ArrayList to LinkedList was pretty much the ONLY change I made in the code when comparing the speeds. Is that the right way to go about it?

EDIT: Oh, and I just do System.currentTimeMillis() before and after the add function to measure time.


Then I just using linkedlist methods ie "Names.add(strings)". And when I tested arraylists, it's nearly identical:

How did you test? Generally it's done by measuring the time it takes to do the same operation millions of time followed by a simple division.


Yeah, you are using those correctly. How much data are you testing with? If the data set is too small, you might not get good test results, because there might be special case optimizations coming into play that skew the results. For your timing, you should average the time over many test runs to be sure CPU load spikes and other resource spikes are averaged out.

I don't know for sure, but I would suggest trying different volumes of test data - maybe increase by factors of 10 and see if you see a linear change in performance. For example, you could test each list with 100 items, 1000, 10000, 100000, and 1000000 and see if the difference is linear and how the implementations compare.

It might be interesting to test the time it takes to insert an item into the middle of the list, in addition to inserting at the beginning and end.


Simple time deltas is fine if you want something simple, however for future reference if you want something more complex check out this:

http://www.ibm.com/developerworks/java/library/j-benchmark1.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜