my dog learned polymorphism*
The moose likes Performance and the fly likes Vector vs. the new collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Vector vs. the new collections" Watch "Vector vs. the new collections" New topic
Author

Vector vs. the new collections

paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20545
    ∞

Apparently Vector is loaded with synchronization stuff that slows things down. How much slower is it, if at all?


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
From what I've heard, ArrayList isup to 4 times faster on common operations.
I have achieved visible speedups on applications by replacing Vector by ArrayList, even though the number of elements in the collection are small (< 100 usually).


42
Jack Shirazi
Author
Ranch Hand

Joined: Oct 26, 2000
Posts: 96
Vector is slower than the corresponding non-synchronized ArrayList class, but faster than using a synchronized wrapper on an ArrayList. The difference in speed is very dependent on the VM and the application. In my book Java Performance Tuning I do a direct comparison of ArrayList, Vector and a synchronized wrapped ArrayList. I find that 1.2 VMs can run ArrayList significantly faster than Vector (I've also seen up to four times speedup), but that the HotSpot VMs can optimize away most of the difference. However, HotSpot VMs are also the most variable in results, sometimes running Vector faster than ArrayList.
The actual test is available as the tuning.threads.ListTesting class, with the rest of the code from my book (just follow the examples link).
The question is in some ways the wrong one to ask. A better question would be "when should I use Vector instead some non-synchronized or other synchronized Vector-like class. And the answer I would give is that you should generally use the java.util.List interface to specify your variable type, and by default assign a Vector, since it is the fastest synchronized List available in the SDK. Then, if your application needs speeding up AND you find that a Vector is causing a bottleneck AND you can replace the particular Vector with an unsynchronized List because of the (possibly single-threaded) way it is used, THEN replace it with an ArrayList.
 
 
subject: Vector vs. the new collections