A huge lack of performance in emscripten code

it's 2:50 in the morning here, and after a busy day I found something strange. I am doing my best to give a picture of my problem.

I wrote these two code snippets 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 (hopefully)

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

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

+6
source share
2 answers

node.js lacks most of the real asm.js performance improvements that quickly create emscripten. Instead of trying it with node, try it in firefox or chrome.

The problem is that node.js tends to lag behind the chrome version of V8, so the features (or optimizations) that come with Chrome may take some time to turn them into V8. I really don't know how long, but the asm.js optimization is 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 is even faster.

+4
source

I think you might have a bug somewhere in your compilation toolchain. Make sure that it does not accidentally include system libraries / headers instead of choosing emscripten. Also make sure that you do not accidentally use your systems.

If you do emcc -v test.cpp (assuming test.cpp is your file that you are compiling), it should specify exactly which headers llvm / clang and node will rely on. Below you can see that the emcc command is faster by default than the cang clang native code (this may seem unexpected, but V8 is run-time optimization, C ++ is 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$ 

Lastly, make sure you are using the latest and greatest update. / emsdk and then. / emsdk install the latest version of 64 bit. This has been tested with node 0.10.21 and emscripten 1.16 on MacOS X Mavericks.

+2
source

All Articles