Java Reference
In-Depth Information
System.out.println (vectime + "\t" + arlisttime +
"\t" + alitime + "\t" + arraytime);
} // doTests
...
The testv() method appears below. The others are similar and appear in the
We b Course material. In each case, the fetches from the containers are in a loop
in order to make the total time large enough to measure. We have to do something
with the Integer objects fetched from the containers or else the compiler will
notice that nothing is changing inside the loop and remove the loop altogether. So
for our timing loop we first cast the Object type retrieved from the container to
an Integer and then we accumulate a total sum of all the values after converting
from Integer type to int type using the intValue() method on Integer .
public long testv () {
// Create Vector and fill elements with Integer objects
Vector vec = new Vector (fNum);
for (int i = 0; i < fNum; i++) {
vec.add (new Integer (i));
}
// Now test access times by looping through the Vector
// to access each Integer element.
// First warmup the hotspot compiler.
long sum = 0;
for (int i = 0; i < fWarmup; i++) {
sum + = ((Integer)(vec.elementAt (i))).intValue ();
}
// And give ittimetofinishthe JIT compile
// in the background.
if (fSleep > 0)
try { Thread.sleep (fSleep);
} catch (InterruptedException e) {}
// Then do the loop for real and time it.
long t1 = System.currentTimeMillis ();
for (int j = 0; j < fOuter; j++) {
for (int i = 0; i < fNum; i++) {
sum + = ((Integer)(vec.elementAt (i))).intValue ();
}
}
long t2 = System.currentTimeMillis ();
Search WWH ::




Custom Search