Cocos2d-x JS Performance test

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.