Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector vs. ArrayList

 
Josh Brown
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joselito Cachaceiro
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Zhui Shen Ge
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic