• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Vector Internals

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,

Thanks for the timely reply.

Regards,
Pradeep
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic