File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Performance and the fly likes Vector vs. ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Vector vs. ArrayList" Watch "Vector vs. ArrayList" New topic
Author

Vector vs. ArrayList

Josh Brown
Ranch Hand

Joined: Oct 09, 2007
Posts: 35
I realize this question's been around forever, but does ArrayList actually perform better than Vector? I heard somewhere that with either Java 5 or 6, the compiler is smart enough not to care that Vector is synchronized and Vector actually performs just as well as ArrayList. Is this true?

I've spent some time googling this and couldn't find an answer. Most of the results are just people's thoughts, and nothing I found specifically addressed the fact that performance may have changed since Java 1.2. Is there a good place to go to look up things like this?


Josh
Inside the Machine
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
As of JDK 6, the JVM may omit unnecessary synchronization if it can determine that only one thread can possibly access the instance being synchronized on. So if you have a Vector referenced only by a local variable, that is probably reachable only by the one thread that is running the method that defines the local variable, so the JVM may ignore synchronization on that Vector. I don't know that it's guaranteed that this will happen - most likely, it isn't. But there's a good chance.

More generally, the best way to find out which performs better in a prticular situation is to test it yourself. But this sort of thing varies depending on the platform and depending on exactly what your program is doing. And in the vast majority of cases it really doesn't matter. I think the best reason to avoid ever using Vector is just to stop the endless line of questions about the difference between the two classes.


"I'm not back." - Bill Harding, Twister
Joselito Cachaceiro
Greenhorn

Joined: Jan 16, 2008
Posts: 13
[Non-responsive post removed. If you want to start a substantive discussion of the relative merits of C++ vs. Java, please start your own topic on the subject.]
[ January 26, 2008: Message edited by: Bear Bibeault ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Another performance difference -- one that's likely to matter more in cases where collection performance is really an issue -- is the algorithms these two classes use to allocate excess capacity.

If you've set the capacityIncrement property, they both behave more or less the same; if you have not, then Vector doubles the array size when it needs more space, while ArrayList multiplies it by 1.5. Doubling actually gives the theoretical best performance for starting from 0 and growing to a large size; multiplying by 3/2 actually tends to work out better in real-world situations where the list can both grow and shrink.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[EFH]: If you've set the capacityIncrement property, they both behave more or less the same;

It looks to me like if you set the capacityIncrement (available only in Vector) to any positive number, you get very different behavior, and generally very poor performance if your list length exceeds the capacityIncrement by a large factor. Which is probably why they abandoned this idea for ArrayList - it's close to useless for any practical application. If you set the capacityIncrement to zero or a negative number, it's as if you never set it at all. You get the standard behavior for Vector, which is what you want.
[ January 27, 2008: Message edited by: Jim Yingst ]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4634
    
    5

Originally posted by Josh Brown:

I've spent some time googling this and couldn't find an answer.


The facts change too often with too many things for their to be a clear answer. Its documented that ArrayList may be slower. That's about all you can say in general.

Also in general, over time, optimizations get better, so equivalent ways of doing the same thing tend to get optimized together and thus the performance approaches identical.

If you application really cares, you have the data to test it, and you'll know for your JVM, OS, hardware, etc. Just expect it to change with the next OS, JVM, etc.
Nikhil Shah Jain
Ranch Hand

Joined: Apr 21, 2009
Posts: 31


gives the following output

Total time for arraylist 985
Total time for vector 390

This shows Vector is faster, but is assumed otherwise.
Can anybody please justify.Its confusing.
Need to do performance tuning on my application.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8705
    
    6

Nikhil L Shah wrote:
Need to do performance tuning on my application.


What benchmark did you use to identify a Vector or ArrayList as the bottleneck? I would be willing to bet the difference in performance in ArrayList and Vector will be dwarfed by many other factors in a non-trivial application.
Have a look at our EnterprisePerformance FAQ for advice on how to identify and address the performance bottlenecks in an application (Note #13).
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 852
The second loop as an advantage as the optimizer might kick in by then and it may already have more of the classes it needs loaded by then. Unless you ran these tests multiple times it is inconclusive.

However, as the previous poster asked. Is this really your performance problem? Does your application do millions of gets and puts? If in the course of the day you do several million at best you would shave a fraction of a second off of your execution time for the whole day.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Zhui Shen Ge
Greenhorn

Joined: Nov 22, 2009
Posts: 5
I've ran the sample code (multiple times) in JDK 6_17 with the following results. Noticed that only the first run will show that Vector is faster :P

Total time for arraylist 219
Total time for vector 421
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
This is my result for the first time itself:


Total time for arraylist 203
Total time for vector 359


I have jdk1.6.0_10, pentium dual core E5300 2.6 GHz.


SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector vs. ArrayList
 
Similar Threads
e-shop cart
advantage Doublylink list over linklist
Vector vs Arraylist
Properties file with multiple values for same key?????
Obsolete collection?