Answer the question
In order to leave comments, you need to log in
Emscripten - where is the performance?
Here for a simple program there are source codes in
C ++
#include <iostream>
using namespace std;
#include <sys/time.h>
#include <unistd.h>
static double GetMilliseconds(){
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec*1000.0 + time.tv_usec/1000.0);
}
int i=0;
int fib(int x) {i++;
if (x < 2) {
return 1;
} else {
return fib(x - 1) + fib(x - 2);
}
}
int main() {
double t0 = GetMilliseconds();
int result = fib(35);
double ms = GetMilliseconds()-t0;
cout << "time ms: "<< ms << "res: "<<i<<"-"<<result<<endl;
cin.get();
return 0;
}
var i=0;
function fib(x) {i++;
if (x < 2) {
return 1;
} else {
return fib(x - 1) + fib(x - 2);
}
}
window.onload = function(){
var time = new Date().getTime();
var result = fib(35);
document.write(new Date().getTime()-time+"<br>res: "+i+"-"+result);
}
Answer the question
In order to leave comments, you need to log in
I tried to write a recursive fibonacci in pure asm.js:
(function(std, env, heap) {
'use asm';
function fib(n) {
n = n|0;
if((n|0) < (2|0)) {
return 1|0;
}
return (fib((n|0) - (1|0)) + fib((n|0) - (2|0)))|0;
}
return fib;
}(
{},
null,
new ArrayBuffer(0)
));
With such questions, you need to look at the assembler listing. I guess gcc threw out all the calculations and planted a constant.
He definitely had to unwind the recursion.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question