Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huge performance lack in emscripten code

it's 2:50 AM here and after a hectic day I found something strange. I do my best to give a picture of my problem.

I wrote these two pieces of code in C++ and JavaScript:

#include<stdio.h>
#include <time.h>

int main() {
    clock_t tStart = clock();

    int result = 0;
    for (int a = 0; a < 1000000000;a++) {
        result += a;
    }

    printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);

  return 1;
}

And:

var start = new Date().getTime();

var result = 0;

for(var a = 0;a < 1000000000;a++) {
    result += a;
}

var end = new Date().getTime();
var time = end - start;
console.log('Time taken: ' + (time/1000) + 's');

Both of them do the same thing (I hope so)

After generating ./a.out.js using the latest version of emscripten, I found something weird: the result of executing both codes

The execution time of the emscripten code is really slower than the manually written JavaScript code. What's the problem?

like image 888
Afshin Mehrabani Avatar asked May 01 '14 22:05

Afshin Mehrabani


1 Answers

node.js lacks most of the real asm.js performance tweaks that make emscripten fast. Instead of trying it with node, try it in firefox or chrome.

The issue is that node.js tends to lag behind chrome's V8 version, so features (or optimizations) that go into Chrome may take quite awhile to make it into V8. I don't actually know how long, but the asm.js optimizations are new enough that when I last tried it in early April, 2014 it was significantly slower on the command line with node.js than in the browser with Chrome, and Firefox was faster still.

like image 82
taxilian Avatar answered Oct 19 '22 21:10

taxilian