Just now, I am testing this with a "compiled" directive.
Simple changes are: code inside very identical functions, except that the compiled one needs to declare the i variable.
Results are:
compare()
Starting non compiled espruino pi at 1514037569001.04614257812
Pi: 3.14159365358
Took 446121180.6640625 Microseconds for 1000002 iterations
Starting COMPILED espruino pi at 1514038015123.26708984375
Pi: 3.14159365358
Took 36048217.7734375 Microseconds for 1000002 iterations
=0
So there is a speedup of a factor 12 to using the "compiled" directive.
I had experimented a comparable speedup in a gps checksum computation 2 years ago.
See here
function testcompiled() {
"compiled";
var iterations = 1000000;
var x = 1;
var pi = 1;
var i=2; // required for compiler
var begin=Date.now();
console.log("Starting COMPILED espruino pi at", begin);
for (i = 2; i < iterations + 2; i++) {
x = x * -1;
pi = pi + (x / (2*i-1));
}
pi = pi * 4;
console.log('Pi: ' + pi);
var end= Date.now();
console.log('Took ' + ((end-begin)*1000) + ' Microseconds for ' + i + ' iterations');
return pi;
}
function test() {
var iterations = 1000000;
var x = 1;
var pi = 1;
var begin=Date.now();
console.log("Starting non compiled espruino pi at", begin);
for (i = 2; i < iterations + 2; i++) {
x = x * -1;
pi = pi + (x / (2*i-1));
}
pi = pi * 4;
console.log('Pi: ' + pi);
var end= Date.now();
console.log('Took ' + ((end-begin)*1000) + ' Microseconds for ' + i +' iterations');
return pi;
}
function compare() {
return test() - testcompiled();
}
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Just now, I am testing this with a "compiled" directive.
Simple changes are: code inside very identical functions, except that the compiled one needs to declare the i variable.
Results are:
So there is a speedup of a factor 12 to using the "compiled" directive.
I had experimented a comparable speedup in a gps checksum computation 2 years ago.
See here