samedi 3 octobre 2020

I know Java Reflection is slow, but why is its execution time so unstable?

I've been testing java reflection and I found that, when using execute(), the time needed to finish the method varies a lot.

Here is the code that I'm using:

int nTest = 101;
long mean = 0;
long[] execTime = new long[nTest - 1];

for (int i = 0; i < nTest; i++) {
     long tStart = System.nanoTime();
     metodin.invoke(o, parameters);
     long tFinal = System.nanoTime();
     long tDiff = (tFinal - tStart) / 100;

     if (i > 0)               // I discard the first execution just in case it takes a lot of time.
         execTime[i - 1] = tDiff;
}

long totalExecutionTime = 0;

for (int i = 0; i < numbers.length; i++) {
    totalExecutionTime += execTime[i];
    // System.out.println(execTime[i]);
}
mean = totalExecutionTime / execTime.length;

And here's one of the multiple examples where we can see a huge variation in time (print obtained using the commented println):

24
23
20
21
21
[...]
22
22
23
60
23
19
20
13028 // BOOM!!!!!!!
496 // and the low performance continues for a while...
160
115
116
120
114
123
121
115
120
114
114
127
323
40 // returns to normal
27
25
23
18
35

The variation is so big that it makes the mean not reflect the reality of the method. To solve this problem I was executing over 1000 times the method, but it seems that this "BOOM" is kind of cyclic. Why is this happening?





Aucun commentaire:

Enregistrer un commentaire