As3 efficiency optimization: using the Vector Data Type

Source: Internet
Author: User

Today, when I changed my colleagues' programs, we had to spend a long time dealing with vector. We used to support Flash Player 10 and later -- so we had a thorough understanding of vector for mobile development, after all, the efficiency is higher than that of array --

Nima-Despise the program of a former colleague-and read it-that is-Dark --

When flashplayer10 is accompanied by the release of flashcs4, as3 adds a new data type: Vector

Here we will give a brief introduction to students who are not familiar with vector (excerpt and adapted from the official documentation ):

Vector is a special array.

  • First, the definition of vector is closer to the "array" in C language, that is, each element of the array has the same data type. (This means that the access to elements in the vector must go through the type check)
  • Second, vector is a dense array, that is, each index must contain a value. (It can be null)
  • Finally, the vector can be specified as a fixed-length array at any time.

Due to these special characteristics, vector has the following advantages:

  • Performance:When a vector instance is used, the access and iteration speed of group elements is much faster than when array is used.
  • Type security:In strict mode, the compiler can identify data type errors. (But when usingpush()Method orunshift()When a value is added to a vector, the compiler does not check the data type of the parameter, but checks it at runtime)
  • Reliability:Compared with array, runtime range check (or fixed-length check) greatly improves reliability.

Today, we are mainly concerned with the use of vector data types to optimize program efficiency.

For efficiency-oriented Ria applications, using vector to improve efficiency is a simple but effective method,

However, many developers do not have a deep understanding of the vector, so they fail to take full advantage of the speed advantage of the vector.

Next we will analyze the best use of vector.

 

Note: The runtime environment of all the Code in the following article: flashplayer 10.1 & Mac OS X 10.6.4. The average time consumption is calculated and rounded up multiple times.

1. The length of the Vector Array should be fixed.

The implementation of the vector type in AVM uses a fixed-length array. Although the default fixed attribute in as3 is false, we do not recommend that you change the length of the Vector Array.

See the following test:

?
123456789101112131415161718 var a:Vector.<uint>=new Vector.<uint>(1000000);var b:Vector.<uint>=new Vector.<uint>(10000000);var t:Number=getTimer();for(var k:uint=0;k<9;k+=1){a.concat(new Vector.<uint>(1000000));}for(var i:uint=0;i<10000000;i+=1){a[i]=i;i=a[i];}trace(getTimer()-t);//////////////////////////////////t=getTimer();for(var j:uint=0;j<10000000;j+=1){b[j]=j;j=b[j];}trace(getTimer()-t);

The operation is also performed on an array with a length of 10 million. The former consumes 850 ms while the latter is 350 ms.

If vector A in the above Code uses 10 million push operations to reach a length of 10 million, the time consumption is changed to 1800 Ms.

(In fact, using the above Code to test the array has the same time-consuming ratio, so no matter which array is used, it is not recommended to change its length frequently)

For the time consumed by the splice operation, see

Quick as a flash: optimization strategies for as3 and flashWe can see that the splice operation of vector is 10 times slower than that of array.
2. No difference in forward/reverse traversal speed

For the array type, the forward/reverse traversal speed of its elements is about 3: 2, that is, reverse traversal is slower, such as the following code:

?
123456789101112131415 var a:Array=new Array(10000000);var b:Array=new Array(10000000);var t:Number=getTimer();for(var i:uint=0;i<10000000;i+=1){ a[i]=i; i=a[i];}trace(getTimer()-t);//////////////////////////////////t=getTimer();for(var j:uint=9999999;j>0;j-=1){ b[j]=j; j=b[j];}trace(getTimer()-t);

The former consumes about 1000 ms, while the latter consumes 1500 Ms.

For vector, the speed difference between the forward and reverse directions does not exist. (If you are interested, you can modify the code above for testing. Here we will draw a conclusion)

3. When vector is used to store data of the primitive type, the efficiency advantage of vector can be fully utilized.

The data types in as3. there are two types of data: primitive and reference. For these two types of data, the efficiency of using vector is also different. (It should be noted that in as3, these two types are essentially reference types, except that the former is a constant object and the latter is a variable object)

The first is to compare the read/write efficiency of the vector and array when storing the primitive data:

?
123456789101112131415 var a:Vector.<uint>=new Vector.<uint>(10000000);var b:Array=new Array(10000000);var t:Number=getTimer();for(var i:uint=0;i<10000000;i+=1){a[i]=i;i=a[i];}trace(getTimer()-t);//////////////////////////////////t=getTimer();for(var j:uint=0;j<10000000;j+=1){b[j]=j;j=b[j];}trace(getTimer()-t);

It takes about 1200 ms to use vector, while ms to use array.

It can be seen that when the primitive type is used, the access speed of vector is three times higher than that of array.

Next, let's take a look at the efficiency comparison when storing reference data:

?
123456789101112131415 var a:Vector.<Object>=new Vector.<Object>(1000000);var b:Array=new Array(1000000);var t:Number=getTimer();for(var i:uint=0;i<1000000;i+=1){a[i]={data:i,label:i};i=a[i].data;}trace(getTimer()-t);//////////////////////////////////t=getTimer();for(var j:uint=0;j<1000000;j+=1){b[j]={data:j,label:j};j=b[j].data;}trace(getTimer()-t);

The time consumption is basically the same,Array consumes 3% more time than vector ~ 8%, the speed is still less than the vector. If the efficiency is not very high, it can be ignored.

Finally, a separate test is performed on the use of the string type, which is consistent with the performance of the reference type.

4. Pay attention to the index data type

Many developers ignore the details, that is, in as3, "what looks like an integer" is not necessarily an integer.

See the following code snippet:

?
123456789101112131415161718192021222324252627282930 var a:Vector.<uint>=new Vector.<uint>(10000000);var t:Number=getTimer();for(var i:uint=0;i<10000000;i+=1){a[i*2/2]=i;i=a[i*2/2];}trace(getTimer()-t);//////////////////////////////////t=getTimer();for(var j:uint=0;j<10000000;j+=1){a[uint(j*2/2)]=j;j=a[uint(j*2/2)];}trace(getTimer()-t);//////////////////////////////////t=getTimer();for(var k:uint=0;k<10000000;k+=1){a[(k*2/2)>>0]=k;k=a[(k*2/2)>>0];}trace(getTimer()-t);//////////////////////////////////t=getTimer();var n:uint;for(var q:uint=0;q<10000000;q+=1){n=(q*2/2);a[n]=q;q=a[n];}trace(getTimer()-t);

The time consumed by the four methods is 1100 ms, 750 ms, 650 ms, and 550 ms respectively. Of course, the last method is not comparable because it is only calculated once. (If it is calculated twice, it is 750 ms)

Here, multiplication and division are used for testing. In addition, the results of + 1/-1 are also the same.

So I 'd like to mention it here,If you must write the formula directly at the index position, you must use bitwise operations to convert it into an integer.

For the use of Vector in as3 efficiency optimization, I can think of these four points. (I don't know if I want to update it later)

Hope to help you.

Attached references http://www.cnblogs.com/zhoujunfeng2011/archive/2011/07/08/2101209.html

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.