Comparison of four kinds of storage containers in AS3 (array,object,vector,dictionary)

Source: Internet
Author: User
Tags comparison

There are four kinds of containers for objects in AS3: Array,object,vector,dictionary; others have mentioned several comparisons of CPU memory or speed in their blogs. The performance is not much worse, but for large projects, or for projects that need to be ported to mobile devices, the optimization is compelling.

First, choose the right type according to your needs.

Array, the general language has, here is not to say, but to know that he is an indexed array, so the index as far as possible to use int, so that you can improve the speed, if the use of strings, then the length method may not be the same as you think (you can test a little bit hehe)

object is the map, which is an associative array. Key values are strings that are more efficient than dictionary in their tests.

Vectors are also indexed arrays, but the advantages are obvious in testing.

Dictionary is a special kind of array. His key value is a reference to the object. So when the content and the key values are contacted, you can use this, but pay attention to his strong references and weak references.

Vectors are the most efficient in their data, but array is what we use, so we weigh the two ourselves with the data:

The following code shows the base of the Array class:

  code is as follows copy code

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 shows the base 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 refine this example by assigning a specific length to a vector and setting its length to a fixed value:

The code is as follows Copy Code

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 you do not specify the size of the vector in advance, the vector size increases after the vector has run out of space. Each time the vector size increases, a new block of memory is allocated. The current contents of the vector are copied into the new memory block. This additional allocation and replication of data can degrade performance. The code above is optimized for performance by specifying the original size of the vector. However, the code is not optimized for maintainability. In order to improve maintainability, store reused values in constants:

The code is as follows Copy Code

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

So try to use the Vector object APIs as much as possible because they 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.