Optimization of Flash Platform Technology (11) Other performance optimizations

Source: Internet
Author: User
Tags addchild

For textfield objects, use the appendtext () method instead of the + = Operator. When using the text attribute of the textfield class, use the appendtext () method instead of the + = Operator. The appendtext () method can improve the performance.
For example, the following code uses the + = Operator to complete a loop in 1120 milliseconds:
Addchild (mytextfield );
Mytextfield. autosize = textfieldautosize. Left;
VaR started: Number = gettimer ();
For (var I: Int = 0; I <1500; I ++)
{
Mytextfield. Text + = "ActionScript 3 ";
}
Trace (gettimer ()-started );
// Output: 1120
In the following example, the + = Operator is replaced with the appendtext () method:
VaR mytextfield: textfield = new textfield ();
Addchild (mytextfield );
Mytextfield. autosize = textfieldautosize. Left;
VaR started: Number = gettimer ();
For (var I: Int = 0; I <1500; I ++)
{
Mytextfield. appendtext ("ActionScript 3 ");
}
Trace (gettimer ()-started );
// Output: 847
Now the code can be completed in 847 milliseconds.


Update text fields outside the loop whenever possible.



This code can be further optimized using simple technologies. Updating text fields in each loop uses a lot of internal processing. Concatenate and allocate only one string
Text fields outside the loop can greatly reduce the time required to run the code. This code can be completed in only 2 milliseconds:
VaR mytextfield: textfield = new textfield ();
Addchild (mytextfield );
Mytextfield. autosize = textfieldautosize. Left;
VaR started: Number = gettimer ();
VaR content: String = mytextfield. text;
For (var I: Int = 0; I <1500; I ++)
{
Content + = "ActionScript 3 ";
}
Mytextfield. Text = content;
Trace (gettimer ()-started );
// Output: 2
When processing HTML text, the previous method is too slow, which may cause a timeout exception in Flash Player in some cases. For example, if the basic hardware speed is too slow, an exception may occur.
Note: Adobe AIR does not cause this exception.
VaR mytextfield: textfield = new textfield ();
Addchild (mytextfield );
Mytextfield. autosize = textfieldautosize. Left;
VaR started: Number = gettimer ();
For (var I: Int = 0; I <1500; I ++)
{
Mytextfield.html text + = "ActionScript <B> 2 </B> ";
}
Trace (gettimer ()-started );
By assigning this value to a string outside the loop, this Code takes only 29 milliseconds to complete:
VaR mytextfield: textfield = new textfield ();
Addchild (mytextfield );
Mytextfield. autosize = textfieldautosize. Left;
VaR started: Number = gettimer ();
VaR content: String = mytextfield.html text;
For (var I: Int = 0; I <1500; I ++)
{
Content + = "<B> ActionScript <B> 3 ";
}
Mytextfield.html text = content;
Trace (gettimer ()-started );
// Output: 29
Note: In Flash Player 10.1, the string class has been improved, so the memory used by the string is small.

 

Avoid using the brackets operator whenever possible.

Using the brackets operator may reduce performance. You can avoid using this operator to store your references in local variables. The following code example demonstrates the low efficiency of using the brackets OPERATOR:
VaR LNG: Int = 5000;
VaR arraysprite: vector. <sprite> = new vector. <sprite> (LNG, true );
VaR I: int;
For (I = 0; I <LNG; I ++)
{
Arraysprite [I] = new sprite ();
}
VaR started: Number = gettimer ();
For (I = 0; I <LNG; I ++)
{
Arraysprite [I]. x = math. Random () * stage. stagewidth;
Arraysprite [I]. Y = math. Random () * stage. stageheight;
Arraysprite [I]. Alpha = math. Random ();
Arraysprite [I]. Rotation = math. Random () * 360;
}
Trace (gettimer ()-started );
// Output: 16
The following optimized versions reduce the use of the brackets OPERATOR:
VaR LNG: Int = 5000;
VaR arraysprite: vector. <sprite> = new vector. <sprite> (LNG, true );
VaR I: int;
For (I = 0; I <LNG; I ++)
{
Arraysprite [I] = new sprite ();
}
VaR started: Number = gettimer ();
VaR currentsprite: SPRITE;
For (I = 0; I <LNG; I ++)
{
Currentsprite = arraysprite [I];
Currentsprite. x = math. Random () * stage. stagewidth;
Currentsprite. Y = math. Random () * stage. stageheight;
Currentsprite. Alpha = math. Random ();
Currentsprite. Rotation = math. Random () * 360;
}
Trace (gettimer ()-started );
// Output: 9


Use Inline code whenever possible to reduce the number of function calls in the code.


It is costly to call a function. Try to move the Inline code to reduce the number of function calls. Mobile Inline code is a good way to optimize code for good performance. But please note that using Inline code may make it difficult to reuse the code and increase the size of the SWF file. Some function calls (such as math class methods) can be easily moved in an in-memory manner. The following code uses the math. Abs () method to calculate the absolute value:
Const max_num: Int = 500000;
VaR arrayvalues: vector. <number> = new vector. <number> (max_num, true );
VaR I: int;
For (I = 0; I <max_num; I ++)
{
Arrayvalues [I] = math. Random ()-math. Random ();
}
VaR started: Number = gettimer ();
VaR currentvalue: number;
For (I = 0; I <max_num; I ++)
{
Currentvalue = arrayvalues [I];
Arrayvalues [I] = math. Abs (currentvalue );
}
Trace (gettimer ()-started );
// Output: 70
The computation executed by math. Abs () can be manually completed and moved in parallel:
Const max_num: Int = 500000;
VaR arrayvalues: vector. <number> = new vector. <number> (max_num, true );
VaR I: int;
For (I = 0; I <max_num; I ++)
{
Arrayvalues [I] = math. Random ()-math. Random ();
}
VaR started: Number = gettimer ();
VaR currentvalue: number;
For (I = 0; I <max_num; I ++)
{
Currentvalue = arrayvalues [I];
Arrayvalues [I] = currentvalue> 0? Currentvalue:-currentvalue;
}
Trace (gettimer ()-started );
// Output: 15
By calling an internal mobile function, code execution speed can be increased by more than four times. This method is applicable to multiple situations, but pay attention to its impact on reusability and maintainability. Note: The code size has a great impact on the overall execution of the player. If an application contains a large amount of ActionScript code, the virtual machine will spend a lot of time verifying the code and JIT compilation. Attribute search speed may be slow because the inheritance level is deep and the internal cache is easy to fail. To reduce the code size, avoid using the Adobe Flex framework, TLF Framework library, or any large third-party ActionScript library.

 

Avoid statements in the calculation cycle.

Statements in the calculation cycle can also be optimized. The following code traverses the array but is not optimized, because the length of the array needs to be calculated during each time:
For (var I: Int = 0; I <myarray. length; I ++)
{
}
It is best to store this value and reuse it:
VaR LNG: Int = myarray. length;
For (var I: Int = 0; I <LNG; I ++)
{
}


Use the opposite order for the while loop.


The speed of the while loop in reverse order is faster than that of the forward loop:
VaR I: Int = myarray. length;
While (-- I>-1)
{
}
These tips provide several methods to optimize the ActionScript, explaining how a single line of code affects performance and memory. There may be many other ActionScript
Optimized. For more information, see the following link: http://www.rozengain.com/blog/2007/05/01/some-actionscript-30-
Optimizations /.

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.