0

My program iterates over a given data and I have observed a strange behavior. The first few samples that the algorithm processes, show a slow run time performance but then the subsequent samples and iterations run at almost a consistent (and with a relatively low run time than the first few samples/iterations).

Why is this so? I even tried to call the function outside of the iterating loop as a warm up function call hoping if the JVM was optimizing the code it would do that with the warm up function call.

// warm up function call
warpInfo = warp.getDTW(testSet.get(startIndex), trainSet.get(0), distFn, windowSize);

this.startTime = System.currentTimeMillis();
for(int i=startIndex; i<endIndex; i++) {
    for(int j=0; j<trainSet.size(); j++) {
        train = trainSet.get(j);
        instStartTime = System.currentTimeMillis();
        warpInfo = warp.getDTW(test, train, distFn, windowSize);
        if(warpInfo.getWarpDistance()<bestDist) {
            bestDist = warpInfo.getWarpDistance();
            classPredicted = train.getTSClass();
        }
        instEndTime = System.currentTimeMillis();
        instProcessingTime = instEndTime - instStartTime;
        // record timiing and results here
    }
    // record other information here
}
Atif
  • 345
  • 1
  • 4
  • 16
  • 1
    Can you please share your code with some data ? We cannot even search for an answer without theses few information. – JFPicard Mar 30 '15 at 17:51
  • 1
    The JVM optimization will need more than a single call to actually perform an optimization... the JVM sees the loop as the hot spot and optimizes it there – Alexis Murray Mar 30 '15 at 17:51
  • So in your opinion, a warm up call is useless in this scenario? It will not have any effect on the code optimization? – Atif Mar 30 '15 at 18:03

1 Answers1

0

After searching, I have come across a few links SO Answer to a similar question and this Java performance comparison which have the option of specifying the -XX:CompileThreshold=1 when running the program to tell the JVM to compile the code after this many function calls. Also see the Oracle Page. After performing a test run with my program, I can state that it solves the problem.

**EDIT: **Based on Zarev's comments, I'd say I was wrong in posting this as an answer. CompieThreshold flag only allows for a premature compiling of the code so it is not causing the larger run time for the first iteration but in reality it may be causing the program to be compiled without considerations for the details.

Community
  • 1
  • 1
Atif
  • 345
  • 1
  • 4
  • 16
  • 1
    That's not a problem at all. It's just how JIT works. Using `-XX:CompileThreshold=1` you are doing more harm than good (actually no good at all). – Svetlin Zarev Mar 30 '15 at 18:47
  • Then is there a JVM switch to compile the bytecodes as soon as possible, other than the mentioned one which will be a better choice. – Atif Mar 30 '15 at 18:57
  • There is no point in compiling the code sooner, because in this way you are preventing the JIT to appropriately profile and optimize it. You can try using `-XX:+TieredCompilation` which will compile your code on several steps – Svetlin Zarev Mar 30 '15 at 19:08