• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector vs. the new collections

 
paul wheaton
Trailboss
Pie
Posts: 21390
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently Vector is loaded with synchronization stuff that slows things down. How much slower is it, if at all?
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jack Shirazi
Author
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic