1

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]);
    }
NikoBellic
  • 15
  • 7
  • What do you mean by "trouble getting a running total". Are you getting an error? – Tripp Kinetics Aug 07 '15 at 16:17
  • 1
    Grab the nano time before you enter the loop and then after you are done with the loop and subtract the first from the second. That is the total time. – Sesame Aug 07 '15 at 16:18
  • You may want to start your education with [Robust Java benchmarking](http://www.ibm.com/developerworks/java/library/j-benchmark1/index.html) series. – PM 77-1 Aug 07 '15 at 16:29
  • See also: [How do I write a correct micro-benchmark in Java?](http://stackoverflow.com/q/504103/642706) – Basil Bourque Nov 20 '16 at 17:51

5 Answers5

3

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

Beri
  • 11,470
  • 4
  • 35
  • 57
  • Why check the time in the loop at all? You should just check the time of the overall loop and divide by the loop iterations. – Daniel Aug 07 '15 at 16:24
  • Correct, that was my thought, but I forgot to remove rest of the code:D Thanks @daniel:) – Beri Aug 07 '15 at 16:29
2

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);
Daniel
  • 4,481
  • 14
  • 34
2

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 );
Sertage
  • 3,123
  • 1
  • 19
  • 26
  • Correct answer. Tip: Put that `5000` in a variable rather than hard-coding in two places, to ensure consistency. Another tip, you can use underscores in literal numbers for better reading. `int limit = 5_000 ;` – Basil Bourque Nov 20 '16 at 17:29
0

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));
NikoBellic
  • 15
  • 7
-1

Change this line:

linked_add_total =+ linked_add_time;

to

linked_add_total += linked_add_time;
talex
  • 17,973
  • 3
  • 29
  • 66
Scott Schechter
  • 609
  • 6
  • 7
  • I'm having a hard time on Google trying to find out what =+ does, but apparently it compiles and is a valid operation – Scott Schechter Aug 07 '15 at 16:31
  • =+ is a mistake, that should not compile in Java, in Java we have =. – Beri Aug 07 '15 at 16:33
  • 1
    It's the same as assigning the positive value of `linked_add_time`. If you replaced the + with a - it would assign the negative value. It's easier to view it as `linked_add_total = +linked_add_time;`. – TNT Aug 07 '15 at 16:36
  • It actually does compile and run (java 8), try it yourself. After playing around with it, it looks like an assignment operator, followed by the sign of the second number: i = 2; j = 5; i =+ j is the same as i = (1* j) = 5; i =- j is the same as i = (-1 *j) = -5; – Scott Schechter Aug 07 '15 at 16:39
  • @ScottSchechter: Think about it this way: `result = +5`; is the same as `result =+ 5;` All its saying is "set result equal to the positive of 5", the space doesn't affect the tokenization in this case. – Daniel Aug 07 '15 at 21:13