wood burning stoves*
The moose likes Performance and the fly likes performance : arraylist vs vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "performance : arraylist vs vector" Watch "performance : arraylist vs vector" New topic
Author

performance : arraylist vs vector

anish jain
Ranch Hand

Joined: Feb 03, 2010
Posts: 129
Is vector slow as compared to arraylist even in single threaded environment ?
if it is , then why?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Vector is slow than ArrayList because method of vector are synchronized and that of ArrayList are not


Life is easy because we write the source code.....
anish jain
Ranch Hand

Joined: Feb 03, 2010
Posts: 129
Pramod P Deore wrote:Vector is slow than ArrayList because method of vector are synchronized and that of ArrayList are not


but how does it matter in a single threaded environment ?
Does calling a synchronized method take more time than non-synchronized method??? please explain
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

If program runs in single threaded environment then time required for calling a synchronized method non-synchronized method is same. Because of synchronized method only one thread can execute that method at a time, but in single thread environment there is only one thread so time required is also same.
anish jain
Ranch Hand

Joined: Feb 03, 2010
Posts: 129
Pramod P Deore wrote:If program runs in single threaded environment then time required for calling a synchronized method non-synchronized method is same. Because of synchronized method only one thread can execute that method at a time, but in single thread environment there is only one thread so time required is also same.


so it means that in single threaded environment both arraylist and vector will yiel same performance
am i right?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

in single threaded environment both arraylist and vector will yiel same performance
am i right?


Yes. Hello Ranchers please correct me if I am wrong
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You're wrong There's a small overhead associated with calling a synchronized method; that small overhead is exactly the same regardless of how many threads there might be in a program (note that all Java programs are multithreaded, because even if you only write code in one thread, the JVM supplies many other threads of its own.) For this reason, in any program, the methods of Vector will be a tiny bit slower than those of ArrayList. But only a tiny bit.

A larger difference comes from the different rescaling algorithms they use -- ArrayList grows in smaller increments, and is generally more efficient.


[Jess in Action][AskingGoodQuestions]
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Thanks Ernest Friedman-Hill for correcting me, I really forgot the thing that the JVM supplies many other threads of its own.
 
wood burning stoves
 
subject: performance : arraylist vs vector
 
Similar Threads
Multiple Thread allow to change single variable
array
type casting
Array List vs Vector
what is the difference between the ArrayList and Vector?