Optimization of Flash Platform Technology (7) vector and array

Source: Internet
Author: User
Tags benchmark

Use the Vector class instead of the array class as much as possible.
Flash Player 10 introduces the Vector class, which provides faster read/write access than the array class.
A simple benchmark illustrates the advantages of the Vector class over the array class. Below Code Display the benchmark of the array class:
VaR coordinates: array = new array ();
VaR started: Number = gettimer ();
For (var I: Int = 0; I <300000; I ++)
{
Coordinates [I] = math. Random () * 1024;
}
Trace (gettimer ()-started );
// Output: 107
The following code displays the reference of the Vector class:
VaR coordinates: vector. <number> = new vector. <number> ();
VaR started: Number = gettimer ();
For (var I: Int = 0; I <300000; I ++)
{
Coordinates [I] = math. Random () * 1024;
}
Trace (gettimer ()-started );
// Output: 72
You can further optimize this example by assigning a specific length to the vector and setting its length to a fixed value:
// Specify a fixed length and initialize its length
VaR coordinates: vector. <number> = new vector. <number> (300000, true );
VaR started: Number = gettimer ();
For (var I: Int = 0; I <300000; I ++)
{
Coordinates [I] = math. Random () * 1024;
}
Trace (gettimer ()-started );
// Output: 48
If the vector size is not specified in advance, the vector size will increase after the space is used up. Each time the vector size increases, a new memory block is allocated. The current content of the vector is copied to the new memory block. This additional distribution and replication of data will reduce performance. The above code optimizes the performance by specifying the original vector size. However, the Code is not optimized for maintainability. To improve maintainability, the reusable values are stored in constants:

// Store the reused value to maintain code easily
Const max_num: Int = 300000;
VaR coordinates: vector. <number> = new vector. <number> (max_num, true );
VaR started: Number = gettimer ();
For (var I: Int = 0; I <max_num; I ++)
{
Coordinates [I] = math. Random () * 1024;
}
Trace (gettimer ()-started );
// Output: 47
Try to use the vector object API as much as possible because they may run faster.

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.