This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Performance and the fly likes Vector performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Vector performance" Watch "Vector performance" New topic
Author

Vector performance

Ashish Bassi
Greenhorn

Joined: Aug 22, 2007
Posts: 11
I have a method which needs to return a Vector object. Now, I realize that I dont really need it to be a Vector here and an ArrayList would do the job too since i dont need a synchronized collection. However, due to framework limitations, the return type must be a vector.

I was wondering if I could improve performance if I add objects to an ArrayList, and convert it into a Vector by doing something like

List list = new ArrayList();
list.add(obj1);
list.add(obj2);
list.add(obj3);
...

return new Vector(list);

Or would simply adding objects to a Vector be better than doing the above?

Can i create a non-synchronized subclass of Vector which I can return in my method?





Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

Adding the Objects directly into Vector instance is enough...

Former approach leads to creation of ArrayList instance redundantly...


SCJP 6 [SCJP - Old is Gold]
Sudheer Bhat
Ranch Hand

Joined: Feb 22, 2011
Posts: 75
From the source of Vector.java


Note:I haven't done any bench marking on whether creating ArrayList and then returning Vector(list) will have any notable performance improvement or not. So I am not really sure which one would be better. I feel returning Vector(list) may be good if the number of elements that needs to be added to the vector is high.
Alexander Kober
Ranch Hand

Joined: Aug 05, 2011
Posts: 32

The (main) performance issue with the vector class (if initialized with the proper size) is the synchronization required while adding elements. However, a modern JVM (up to date 1.6+) will inspect your code and identify that you're just accessing a local instance without any chance of another thread interfering. Therefore, it will remove the synchronization overhead at runtime, so can probably just ignore the performance considerations in this case and directly add to the Vector instance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector performance
 
Similar Threads
Vector is faster
how to remove object of perticular class from ArrayList..?
difference between ArrayList - Vector
difference between synchronized list and vector
Convert ArrayList to List