Kilsu Kim
5 views (last 30 days)

Just for Fun and Curiosity: Guess the MATLAB Compile Threshold on Windows

Kilsu Kim on 19 Oct 2023
Latest activity Reply by Walter Roberson on 19 Oct 2023

Recently, I came across a post about the JIT compiler on this Korean blog. In the post. The writer discussed the concept of the "Compile Threshold" and how it is calculated.
"The JVM accumulates the number of calls for each method called and compiles when the number exceeds a certain number. In other words, there is a standard for checking how often it is called and then deciding, 'It is time to compile.' This standard is called the compilation threshold. But what is this and why should it be used as a standard?"
The concept of the "Compile Threshold," as used above, seems to be more commonly associated with Tracing just-in-time compilation.
The writer used the simple Java code below to calculate the threshold.
for (int i = 0; i < 500; ++i) {
long startTime = System.nanoTime();
for (int j = 0; j < 1000; ++j) {
new Object();
long endTime = System.nanoTime();
System.out.printf("%d\t%d\n", i, endTime - startTime);
Since the MATLAB execution engine uses JIT compilation, I just wanted to perform the same experiment that the writer did.
I experimented using simple codes based on the code in the blog. I iterated a function 500 time using for-loop and calculated the execution time for each iteration using tic and toc. Then I plotted the execution time for each loop as blow. First five execution times are much higher than followings (10 times!) The test is very rough so I am not sure that I can conclude "MATLAB has Compile Threshold and it is 5!" but this value is actually correct ;-)
t0 = 0;
tfinal = 10;
y0 = [20;20];
timeToRun = zeros(500,1);
for i = 1:500
tStart = tic;
[preypeaks,predatorpeaks] = solvelotka(t0, tfinal, y0);
tEnd = toc(tStart);
timeToRun(i) = tEnd;
Walter Roberson
Walter Roberson on 19 Oct 2023
My timing experiments typically suggest that the threshold is either 2 or 3 -- but occasionally 5 has shown up.
The published documentation says that, at least for functions, the code goes through the Execution Engine optimization at the time it is parsed, that there is no separate non-optimized initial run for functions.
I have not seen any similar documentation for scripts, but the processing of names used as both variables and functions (no longer permitted in one scope) implied a fair bit that scripts too now go through the Excecution Engine optimization at parse time.
Thus, my timing tests suggesting that the initial iteration timing was higher for reasons not related to initial parse time -- those results have long suggested that the published remarks about Execution Engine optimization were incorrect (or at least misleading.)

See Also


No tags entered yet.