Why is i=i+1 faster than i++?

后端 未结 5 631
野性不改
野性不改 2021-01-13 09:04

Test this code in Flash:

var i:int = 0;
for (var j:int = 0; j < 5000000; j++)
{
    i=i+1;
}// use about 300ms.

i = 0;
for (var j:int = 0; j < 5000000         


        
5条回答
  •  暖寄归人
    2021-01-13 09:23

    I don't have a response to your question but the following has been the fastest among all the loops I tried.

    import flash.utils.getTimer;
    
    var t:int = getTimer();
    var i:int = 0, j:int = -1;
    while (++j < 5000000) {
        i += 1;
    }
    trace(getTimer()-t)
    

    This gives me 83 ms.

提交回复
热议问题