File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes Vector Internals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Vector Internals" Watch "Vector Internals" New topic

Vector Internals

Pradeep Arun

Joined: Dec 28, 2007
Posts: 19

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));

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?

marc weber

Joined: Aug 31, 2004
Posts: 11343

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.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
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.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Pradeep Arun

Joined: Dec 28, 2007
Posts: 19

Thanks for the timely reply.

I agree. Here's the link:
subject: Vector Internals
jQuery in Action, 3rd edition