NikoBellic
NikoBellic

Reputation: 15

Averaging the time of a For loop iteration in Java

I am trying to get the average time that it takes to add each element to an array of 5000 elements. I can get the time of each add operation in the loop, but am having trouble getting a running total so that I can calculate the average outside of the loop.

So far, i have the following code where I am printing the total

for (int i = 0; i < testData.length; i++) {
        testData[i] = UUID.randomUUID().toString();
    }

    /****************************
     * Linked List Set Unit Test
     ****************************/

    long linked_add_time = 0;
    long linked_add_total = 0;

    for (int i = 0; i < 5000; i++){ // first 5000 elements used to measure average add time
        linked_add_time = System.nanoTime();
        linked.add(testData[i]);
        linked_add_time = System.nanoTime() - linked_add_time;
        linked_add_total =+ linked_add_time;
    }
    System.out.println(linked_add_total);

    for (int i = 5000; i < testData.length; i++){
        linked.add(testData[i]);
    }

Upvotes: 1

Views: 1691

Answers (5)

Scott Schechter
Scott Schechter

Reputation: 639

Change this line:

linked_add_total =+ linked_add_time;

to

linked_add_total += linked_add_time;

Upvotes: -1

NikoBellic
NikoBellic

Reputation: 15

I didn't even think about just getting the total time outside of the loop. I was able to do this. Thanks for the input

long startTime = System.nanoTime();
    for (int i = 0; i < 5000; i++){ // first 5000 elements used to measure average add time
        linked.add(testData[i]);
    }
    System.out.println("Average time for adding to LinkedList: " +((System.nanoTime() - startTime)/5000));

Upvotes: 0

Beri
Beri

Reputation: 11620

There is no sence in checking each loop, because this time is so little, that it will be close to zero. And secondly you are not memorizing each loop time anyway. So check time before and after loop, than divide be number of loops.

long startTime = System.nanoTime();

for (int i = 0; i < 5000; i++){
    linked.add(testData[i]);
}
System.out.println("Average time: " +(startTime + System.nanoTime())/5000 );

If you are using LinkedList, then add time should be the same, no matter how big your collection will be. If you would use ArrayList instead than each next add operation can be slower (ArrayList should rebuild inner array, every n new elements added).

Upvotes: 3

Sertage
Sertage

Reputation: 3263

So simple, get the initial and end times and divide per 5000 than you have the average:

long startTime = System.nanoTime();
for (int i = 0; i < 5000; i++){ 
    linked.add(testData[i]);
}
long endTime = System.nanoTime();
System.out.println("Average time: " +(endTime - startTime)/5000 );

Upvotes: 2

Daniel
Daniel

Reputation: 4549

Instead of measuring individual adds, measure the difference from "before the loop" and "after the loop", and divide by loop size to get the value:

final int count = 5000;
final long startTime = System.nanoTime();
for (int i = 0; i < count; ++i) {
    linked.add(testData[i]);
}
final long endTime = System.nanoTime();
final long totalTime = endTime-startTime;
System.out.printf("Total Time = %dns.  Avg Time: %dns\n", totalTime, totalTime/count);

Upvotes: 2

Related Questions