Skip to content

Instantly share code, notes, and snippets.

@fanoush
Last active November 3, 2022 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanoush/ca350c56d0b6847e223fa9cbb877286b to your computer and use it in GitHub Desktop.
Save fanoush/ca350c56d0b6847e223fa9cbb877286b to your computer and use it in GitHub Desktop.
fibo benchmark of InlineC vs compiled JS vs interpreted (for details see https://www.espruino.com/InlineC )
// this runs native and fully in SRAM
// larger n will overflow 32bits, we don't care
var c = E.compiledC(`
// int fibo(int)
int fibo(int n){
if(n <= 1){
return n;
}
int fibo = 1;
int fiboPrev = 1;
for(int i = 2; i < n; ++i){
int temp = fibo;
fibo += fiboPrev;
fiboPrev = temp;
}
return fibo;
}
`);
// native but lot of JS runtime in flash
function compjsfibo(n){
"compiled";
if(n <= 1){
return n;
}
var fibo = 1;
var fiboPrev = 1;
for(var i = 2; i < n; ++i){
var temp = fibo;
fibo += fiboPrev;
fiboPrev = temp;
}
return fibo;
}
//fully interpreted by JS runtime in flash
function jsfibo(n){
if(n <= 1){
return n;
}
var fibo = 1;
var fiboPrev = 1;
for(var i = 2; i < n; ++i){
temp = fibo;
fibo += fiboPrev;
fiboPrev = temp;
}
return fibo;
}
function bench(n,func){
var res=0,t=getTime();
res=func(n);
t=getTime()-t;
print("time:",t*1000,"result=",res);
}
function benchall(n){
[c.fibo,compjsfibo,jsfibo].forEach((f)=>bench(n,f));
}
benchall(200);
/*
STM32F103, 72Mhz
>benchall(200);
time: 0.40411949157 result= -552082539
time: 10.68615913391 result= 2.80571172992e+41
time: 130.16414642333 result= 2.80571172992e+41
>benchall(2000);
time: 0.70190429687 result= 1392522469
time: 103.10864448547 result= Infinity
time: 1273.37217330932 result= Infinity
=undefined
>benchall(20000);
time: 3.71646881103 result= 936372485
time: 1015.77520370483 result= Infinity
time: 12676.12385749816 result= Infinity
APM32F103, 96Mhz
>benchall(200);
time: 0.37789344787 result= -552082539
time: 10.35118103027 result= 2.80571172992e+41
time: 124.66263771057 result= 2.80571172992e+41
>benchall(2000);
time: 0.59556961059 result= 1392522469
time: 100.51822662353 result= Infinity
time: 1219.64669227600 result= Infinity
=undefined
>benchall(20000);
time: 2.85291671752 result= 936372485
time: 990.22006988525 result= Infinity
time: 12141.72148704528 result= Infinity
AIR32F103, 72Mhz
>benchall(200);
time: 0.46920776367 result= -552082539
time: 7.62367248535 result= 2.80571172992e+41
time: 101.34482383728 result= 2.80571172992e+41
>benchall(2000);
time: 0.76866149902 result= 1392522469
time: 71.71988487243 result= Infinity
time: 985.62550544738 result= Infinity
=undefined
>benchall(20000);
time: 3.77774238586 result= 936372485
time: 705.31296730041 result= Infinity
time: 9807.07502365112 result= Infinity
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment