Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Efficiency - Vectors

Thread view: 
Jason Cavett - 17 Apr 2007 19:15 GMT
So, I've been researching how inefficient (performance-wise) Vectors
are against any other Collection that is not synchronized.
Unfortunately, it seems as if everybody has a different opinion.

I'm wondering - how inefficient are Vectors in Java 1.5 versus non-
synchronized Collection classes?  Can anybody point me to an actual
study (other than people spouting off random numbers...anywhere from
2x to 10x's worse).  Would it be worth it to refactor an application
I'm working on to switch from Vectors to something else (ArrayList)?

Does it not matter at all?

Thanks
Daniel Pitts - 17 Apr 2007 21:01 GMT
> So, I've been researching how inefficient (performance-wise) Vectors
> are against any other Collection that is not synchronized.
[quoted text clipped - 9 lines]
>
> Thanks

Actually, it depends entirely upon your use cases.  However, Vector is
more or less deprecated now, and can be replaced by wrapping any other
type of collection in a synchronized collection wrapper
(Collections.synchronized*)

There are performance benefits if the list is used only from a single
thread. The exact amount of benefit I'm not certain.

Often, using Vector as a thread safe collection doesn't actually
protect against thread bugs, it only masks them. I suggest reading
Java Concurrency in Practice <http://www.javaconcurrencyinpractice.com/
> for more information about performance and thread safety.

When ever I see "Vector" in a java project, I automatically think
"Legacy code".  Even if you need a synchronized collection, it would
be better to use the appropriate wrapper classes.

Hope this helps,
Daniel.
Jason Cavett - 17 Apr 2007 21:20 GMT
> > So, I've been researching how inefficient (performance-wise) Vectors
> > are against any other Collection that is not synchronized.
[quoted text clipped - 30 lines]
> Hope this helps,
> Daniel.

Great post.  Thank you.

Since I didn't realize that Vector's are "more or less" deprecated,
are there any other Collection classes that are also in this status?
Joshua Cranmer - 17 Apr 2007 22:13 GMT
> Since I didn't realize that Vector's are "more or less" deprecated,
> are there any other Collection classes that are also in this status?

Vector, Hashtable, and Enumeration are the only ones I can think of off
the top of my head. (shudder: Enumeration's painful method names)
Arne Vajhøj - 18 Apr 2007 00:57 GMT
> So, I've been researching how inefficient (performance-wise) Vectors
> are against any other Collection that is not synchronized.
[quoted text clipped - 5 lines]
> 2x to 10x's worse).  Would it be worth it to refactor an application
> I'm working on to switch from Vectors to something else (ArrayList)?

Jack Shiraz has some tests in "Java Performance Tuning".

You can also write your own test.

There are still a big relative difference.

> Does it not matter at all?

Most likely - not at all.

I consider it very unlikely that a real world app would use
collections so extensively that the choice between ArrayList
and Vector will effect overall app performance.

Choose ArrayList because it is the recommended (the standard) - not
because it is faster.

Arne
Lew - 18 Apr 2007 02:34 GMT
Jason Cavett wrote:
>> Would it be worth it to refactor an application
>> I'm working on to switch from Vectors to something else (ArrayList)?

> Choose ArrayList because it is the recommended (the standard) - not
> because it is faster.

Choice of collection implementation can be influenced by performance
characteristics in the big-O sense, as well as by other characteristics such
as sortedness.

The API docs for different collections implementations generally comment on
whether they act in constant time, linearly or otherwise with respect to
operations like add(), remove(), indexing and iteration.  If you know your app
profile generally favors, say, retrieval over insertion, or that you need
indexed random access rather than sequential, such things might matter.

This leads to the recommended practice of usually declaring the /variable/ to
be of interface type, while implementing the /object/ with a class that has
desirable characteristics, e.g.,

List<String> words = new TreeList<String>();

The algorithm depends only on the abstract behaviors of any List, thus will be
correct with any, but takes advantage of whatever it is TreeList does that you
like.  Later, you could revise with LinkedList and not change correctness at all.

The reason to reject Vector is not performance but correctness.  It was
pointed out that its synchronization doesn't guarantee thread-safety in all
circumstances, and for single-threaded use its synchronization is unnecessary.
 It is the characteristic of synchronization that is not desired here, not
its slowness.

Signature

Lew

Arne Vajhøj - 22 Apr 2007 03:04 GMT
> Jason Cavett wrote:
>>> Would it be worth it to refactor an application
[quoted text clipped - 13 lines]
> that you need indexed random access rather than sequential, such things
> might matter.

Correct but not particular relevant in the Vector versus ArrayList
discussion.

> The reason to reject Vector is not performance but correctness.  It was
> pointed out that its synchronization doesn't guarantee thread-safety in
> all circumstances, and for single-threaded use its synchronization is
> unnecessary.  It is the characteristic of synchronization that is not
> desired here, not its slowness.

Vector is not incorrect. It is just doing something not
particular usefully.

The reason not to use it is because it is an old class that has
been replaced by ArrayList.

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.