Cameron
Cameron

Reputation: 1918

Java Performance: Getting and Setting Lists

Can anyone tell me if either of these will perform better than the other compiled under Java 1.6? Assume MyObject is a class with one field called listField that has a getter and setter

Sample #1:

MyObject obj = new MyObject();
List<String> lst = new ArrayList<String>(1);
lst.add("Foo");
obj.setListField(lst);

Sample #2:

MyObject obj = new MyObject();
obj.setListField(new ArrayList<String> (1));
obj.getListField().add("Foo");

My thinking is that the creation of a local instance of ArrayList will create memory overhead, but calling getListField() whenever you want to add to the list isn't as fast as accessing a local version of the list. Maybe if there are several items to be added to the list, Sample #1 is faster but with only a few items Sample #2 is faster? Or will the compiler optimize this so that calling getListField() is equivalent to accessing a local version of the list?

Upvotes: 2

Views: 198

Answers (5)

Same number of new's => Same performance.

You shouldn't worry about this unless profiling shows this to be a bottleneck

Upvotes: 0

Tom
Tom

Reputation: 45114

I think that the thing to consider is why are you creating an array list with only one element. That way, when you add new elements to that list, the internal array will have to be expanded, which I consider it to be far worse.

By default, ArrayLists are set to 10 elements.

Upvotes: 0

Amir Afghani
Amir Afghani

Reputation: 38531

Cameron,

Why are you creating an ArrayList and explicitly sizing it to 1 element? If it will never have more than 1 element in it, don't use a List -- if it will, your performance will be trashed by the list resizing. The issue you brought up, imho, is a non issue.

Upvotes: 0

naikus
naikus

Reputation: 24472

IMO the compiler would be smart enough to optimize these two and would make no difference at all.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500535

There's no such thing as a "local instance". Assuming setListField() and getListField() are trivial methods, there should be pretty much no performance difference - if any - between these samples. Assuming you're using HotSpot and no subclasses have been loaded which override setListField or getListField, I'd expect the methods to be inlined.

You need to understand that when you call setListField() (again, assuming it's a trivial implementation) you're not creating a copy of the list, or anything like that. You're just passing a reference to the list object. That reference will then be copied into the field. Very cheap. Likewise I'd expect getListField() to simply return the value of the field - a reference to a list. The only thing that gets copied is a reference - 4 or 8 bytes, probably, depending on your JVM.

Personally the first sample looks like cleaner code to me, but you should concentrate on understanding what's going on here as a rather more important matter than performance.

Upvotes: 9

Related Questions