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.