Idle to do nothing to study the performance of Cocos2dx-lua and Cocos2dx-js comparison, in the performance of the JS test found some interesting phenomenon.
varCalculatetime =function(Name, func) {vartimes= 0, count=100; for(varI= 0;i<count;i++) { varBT =Date.now (); Func (); varET =Date.now (); Times+ = et-T; } Times/= count; Cc.log ("==================================="); Cc.log (Name+ "Use time:" +Times ); Cc.log ("===================================");};varTestplus =function () { for(vari = 0; I <= 100000; i++) { varx = i+500; }};varTestmultiplication =function () { for(vari = 0; I <= 100000; i++) { varx = i * 500; }};varTestdivision =function () { for(vari = 0; I <= 100000; i++) { varx = i/500; }};varTestmax =function () { for(vari = 0; I <= 100000; i++) { varx = Math.max (i,500); }};varTESTMAX2 =function () { for(vari = 0; I <= 100000; i++) { varx = i>500?i:500; }};varTestdisplacement =function () { for(vari = 0; I <= 100000; i++) { varx = i/1000; }};varTestDisplacement2 =function () { for(vari = 0; I <= 100000; i++) { varx = i>>10; }};calculatetime ("Testplus", Testplus); Calculatetime ("Testmultiplication", testmultiplication); Calculatetime ("Testdivision", testdivision); Calculatetime ("Testmax", Testmax); Calculatetime ("TestMax2", TESTMAX2); Calculatetime ("Testdisplacement", testdisplacement); Calculatetime ("TestDisplacement2", TestDisplacement2);
The output is:
JS: ===================================Js:testplus Use time:4.77JS:===================================JS:===================================js:testmultiplication Use time:4.76JS:===================================JS:===================================js:testdivision Use time:4.68JS:===================================JS:===================================Js:testmax Use time:12.48JS:===================================JS:===================================js:testmax2 Use time:5.4JS:===================================JS:===================================js:testdisplacement Use time:4.81JS:===================================JS:===================================Js:testdisplacement2 Use time:4.75JS:===================================
More curious 2 questions, first, is why Math.max is so consumed by performance, second, why is the shift operation faster than the division operation?
On the first question, I went on to test it:
var function (x, y) { return x>y? x:y;}; var function () { for (var i = 0; I <= 100000; i++) { var x = max (i, }};
The result is:
JS: ===================================15.34===================================
It seems that time is mostly spent on function calls.
Then, on the second question, check the data found that the JS calculation using double-precision floating point number, to perform bit operations, need to convert to integer type, and then the operation, resulting in efficiency decline, it seems that this is common sense =
Attach some links:
Http://www.ruanyifeng.com/blog/2010/01/12_javascript_syntax_structures_you_should_not_use.html
http://jerryzou.com/posts/do-you-really-want-use-bit-operators-in-JavaScript/
Cocos2d-x JS Performance test