0

i am using System.nanoTime() to measure elapsed time or runtime in java. But in this case, i tried to measure several elapsed time using a loop. But, every time the loop is continues, the time became faster. While i expecting the values to be close each other. Here i attach my code.

int dataSize = 6, popSize = 20, maxGen = 200;
double pc = 0.5, pm = 0.05;
double[] time = new double[testsample];
double start, end;
data = inisiasiData(dataSize);

for (int i = 0; i < testsample; i++) {

    start = System.nanoTime();
    Genetic_Controller gc = new Genetic_Controller(popSize, pc, pm, maxGen);
    gc.goOptimise(data, dataSize, hybrid);
    System.out.println(gc.getOptimumPrice() + " ");
    end = System.nanoTime();
    time[i] = (end - start) / 1000000;

   }

   System.out.println();
   for (int i = 0; i < testsample; i++) {
        System.out.println(time[i]+" ms");
   }

And below is the result ( 3 time test ).

Test 1 :

173 
173 
173 
173 
173 
173 
237 
173 
214 
173 

14.216628 ms
4.589741 ms
2.873837 ms
3.148624 ms
3.2326 ms
2.918157 ms
2.704486 ms
1.832538 ms
1.579211 ms
1.588542 ms
===================================================================
Test 2 :
173 
173 
173 
293 
173 
173 
173 
274 
173 
173 

1.976229 ms
1.296026 ms
2.476351 ms
2.294404 ms
2.714749 ms
2.268745 ms
2.263146 ms
2.18057 ms
2.282274 ms
2.313999 ms
===============================================================
Test 3 :
173 
213 
173 
173 
173 
173 
173 
173 
173 
173 

1.982294 ms
1.468642 ms
1.143003 ms
1.135072 ms
1.153266 ms 
1.239109 ms
1.185925 ms
1.00351 ms
0.977851 ms
1.044565 ms

Your help means a lot to me. Thanks.

  • 1
    It's probably because of JVM warm-up. Check Rule#1 on the first answer here: https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – ernest_k Oct 14 '18 at 05:37
  • i'm sorry sir, i'm not to involved in JVM, i just a newbie. Can you give me more detail answer ? thank you very much. – La Ode Muhammad Farhan Fauzan Oct 16 '18 at 13:06
  • There's a detailed answer at the link given above. Take a look through the answers and you'll understand – ernest_k Oct 16 '18 at 13:07

0 Answers0