admin管理员组

文章数量:1323503

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 execution time of the emscripten code is really slower than the manually written JavaScript code. What's the problem?

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 execution time of the emscripten code is really slower than the manually written JavaScript code. What's the problem?

Share asked May 1, 2014 at 22:11 Afshin MehrabaniAfshin Mehrabani 35k33 gold badges144 silver badges209 bronze badges 6
  • Have you tried looking at the javascript generated by emscripten? – SimpleJ Commented May 1, 2014 at 22:16
  • @SimpleJ No, I do believe it's kinda useless. – Afshin Mehrabani Commented May 1, 2014 at 22:17
  • 2 I don't know much about emscripten, but I would assume you would get more answers by paring the outputted javascript to your handmade javascript then by paring your handmade javascript and C++. – SimpleJ Commented May 1, 2014 at 22:20
  • 1 You should also try posting to the emscripten-discuss Google Group, they are very responsive groups.google./forum/#!forum/emscripten-discuss – rajsite Commented May 1, 2014 at 22:31
  • 1 V8 does a lot more optimization than naive c++, not sure how good the asm piler is... – dandavis Commented May 1, 2014 at 22:42
 |  Show 1 more ment

2 Answers 2

Reset to default 5

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 mand line with node.js than in the browser with Chrome, and Firefox was faster still.

I think you may have a bug somewhere in your pile toolchain. Make sure its not accidentally including the system libraries/headers instead of emscripten's choice. Also make sure you're not accidently using your systems clang.

If you do emcc -v test.cpp (assuming test.cpp is your file your piling) it should tell you exactly what headers, llvm/clang and node its going to rely on. Below you can see the emcc default pile actually runs faster than out-of-the-box clang native c code (this may seem surprising but V8 does runtime optimizations, C++ does not.)

slcmew-nmx2499:Downloads trevor.linton$ gcc test.cpp
slcmew-nmx2499:Downloads trevor.linton$ ./a.out
Time taken: 2.33s
slcmew-nmx2499:Downloads trevor.linton$ emcc test.cpp
slcmew-nmx2499:Downloads trevor.linton$ node a.out.js
Time taken: 1.17s
slcmew-nmx2499:Downloads trevor.linton$ 

Finally make sure you're using the latest and greatest ./emsdk update and then ./emsdk install latest-64bit. This was tested with node 0.10.21 and emscripten 1.16 on MacOS X Mavericks.

本文标签: javascriptHuge performance lack in emscripten codeStack Overflow