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 Internals

 
Pradeep Arun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the following code

Vector v = new Vector();
long time1 = 0;
long time2 = 0;

long timeStarts = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {

time2 = time1;
time1 = System.currentTimeMillis();
System.out.println("Iteration # "+ i +" Time taken = "+(time1-time2));
v.add("Myname");

}
System.out.println("Completed the iteration in" + (System.currentTimeMillis() - timeStarts));

I am just trying to find how much time it takes for the loop execution for the following scenarios

By initalizing the vector with 1000. The output i got is some 16 millisecs. The output is not consistent and i also tried to check, which iteration is taking the time. For the first execution, the 7th iteration takes 2 ms and then 61 takes some millisecs and so on. Even this is not consistent. I just want to know why the output is not consistent and also why it is taking a time for a particular iteration. It is because of memory allocation each time or arraycopying or for garbage collection?

Regards,
Pradeep
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

My guess is that your CPU just had something else to do at those particular milliseconds. After all, that Java program is not the only thread running on your machine.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The by far slowest code in your loop is the println. And there are many reasons why it's performance could be inconsistent:

- scrolling needs to be performed,
- other background process / programs taking CPU time,
- the timer not being precise enough for this kind of benchmark,
- hot spot optimization taking place,
- etc. pp.

In general, micro-benchmarking is quite an art.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is no chance whatsoever that your code will yield any useful performance information.

The System.currentTimeMillis() has a maximum resolution of only milliseconds. A computer can do a heck of a lot in a millisecond: much more than just add an item to a Vector! But it's worse than that, because some implementations of Java don't actually update the clock every millisecond; they jump every few milliseconds.

As already mentioned System.out.println() will always take significant time and can take a long time, if there is scrolling to do, for instance. Never do input/output inside a computational loop that you're trying to time.

And again as already mentioned, ordinary computers running ordinary Java just are nothing like as predictable as you imagine. Java code takes different amounts of time to run the same statements depending on many things, including HotSpot compilation.
 
Pradeep Arun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

Thanks for the timely reply.

Regards,
Pradeep
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic