• Post Reply Bookmark Topic Watch Topic
  • New Topic

performance : arraylist vs vector  RSS feed

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is vector slow as compared to arraylist even in single threaded environment ?
if it is , then why?
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector is slow than ArrayList because method of vector are synchronized and that of ArrayList are not
 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
author and iconoclast
Sheriff
Posts: 24219
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest Friedman-Hill for correcting me, I really forgot the thing that the JVM supplies many other threads of its own.
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!