












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













long startTime = System.currentTimeMillis();
// gives time in milliseconds since 1/1/1970 GMT
// code to be timed comes here
long elapsedTime = System.currentTimeMillis()
long startTime = System.currentTimeMillis();
long counter;
do {
counter++; doSomething(); } while (System.currentTimeMillis() - startTime < 1000)
long elapsedTime = System.currentTimeMillis()
float timeForMethod =
((float) elapsedTime)/counter;
Now accuracy is 10%.
first reading may be just about to change to startTime + 100
second reading may have just changed to finishTime
so finishTime - startTime is off by 100ms
Examining remaining cases, we get
trueElapsedTime =
finishTime - startTime +- 100ms
To ensure 10% accuracy, require
elapsedTime = finishTime – startTime
= 1sec
long startTime = System.currentTimeMillis();
long counter;
do {
counter++; InsertionSort.insertionSort(a); } while (System.currentTimeMillis() - startTime < 1000)
long elapsedTime = System.currentTimeMillis()
float timeForMethod =
((float) elapsedTime)/counter;
do {
counter++; startTime = System.currentTimeMillis(); doSomething();