AS3 Tutorial: for each in resolution Efficiency

Source: Internet
Author: User

New for each... Besides traversing XML, in can also be used to traverse arrays and objects. Create "millions of troops ":

VaR testarr: array = new array ();

For (var I: Number = 0; I <1000000; I ++)

{

Testarr. Push (I );

}

Previous for and... In and the new for each... In loop:

// For 260 milliseconds

VaR Len: Number = testarr. length;

For (var j: Number = 0; j <Len; j ++)

{

Testarr [J] ++;

}

//... In 8000 milliseconds

For (var k: String in testarr)

{

Testarr [k] ++;

}

// For each... In 58 Ms

For each (var m: Number in testarr)

{

M ++;

}

How efficient is it? After multiple tests, the average length of the For Loop is 260 milliseconds (the average length of the non-Pre-stored array is 400 milliseconds),... The average in cycle is 8000 milliseconds, for each... In loop 58 Ms !! So try to avoid... In, for each... In, if you use a for loop, it is best to prestore the array length (VAR Len: Number = testarr. length ;).

Object,... Average in 560 ms, for each... The average in time is 8 ms.

VaR testobj: Object = new object ();

For (var I: Number = 0; I <100000; I ++)

{

Testobj [I] = I + 1;

}

//... In reads tags in 560 milliseconds

For (var j: String in testobj)

{

Testobj [J] ++;

}

// For each... In direct read value: 8 ms

For each (var k: Number in testobj)

{

K ++;

} From http://www.jinflex.com/index.php/archives/91

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.