ActionScript in the array operation (1): PUSH (POP) and unshift (Shift)

xiaoxiao2021-03-06  37

In AS, the array operation is one of the longest uses, and the AS in the ammeter set function is more powerful. One can store any object / type and can also be complete, and it is still unprepared, can be arbitrary Add deletion (similar to ArrayList in Java)

The array of AS provides the addition and deletion of array elements, PUSH / POP UNSHIFT / SHIFT

Where: push is added to the last element from the array, and the POP is just the opposite of the deletion of the last element of the array.

Unshift is added an element in front of the first element of the array, and Shift is the first element of the array.

With the four commands of the array, we usually use it very convenient. If you add deletion, there is no problem, but you need to think about it, especially in front of the previous operation, is it equivalent? • Is the efficiency, first compare the following procedure:

Var Temparray = New Array ();

Varleth =

100000;

VAR TIME1 = GetTimer ();

FOR (var i = 0; i

Temparray.push (i);

}

TRACE (GetTimer () - TIME1); //

1415Time1 = GetTimer ();

While (Temparray.Length> 0) {

Temparray.pop ();

}

TRACE (GetTimer () - TIME1); //

798

The above is an array, which requires 1415 milliseconds for an array push100000 integer element, while POP is required to take 798 milliseconds. It seems that the efficiency is quite high, with TemParray [i] = i; assignment efficiency is also

Try then UNSHIFT below:

Var Temparray = New Array ();

Varleth =

1000;

VAR TIME1 = GetTimer ();

FOR (var i = 0; i

Temparray.unshift (i);

}

TRACE (GetTimer () - TIME1); //

901

Time1 = getTimer ();

While (Temparray.Length> 0) {

Temparray.shift ();

}

TRACE (GetTimer () - TIME1); //

788

Ah, unshift / shift is faster? The brother looks at how much it is cycled, it is not a quantitude, how many times the phase is not known, and more tests will be easily found, unshift / shift will with arrays Increased performance declines rapidly, and Push (POP) will not

So if you need to add or remove a lot of data to the array, don't use unshift (Shift), and if you need to use Reverse to transfer the array, you must It's a lot of times, because the array of reversal is very fast

Of course, if you add a small amount of information, you must add it to the front or use unshift.

Why is the performance of both? It seems to be similar to the operation, but in fact, the memory operations of both doctors are indeed a big difference, because the array is to point to a memory address, while Push (PUP), the entire array The memory will not change, only one record is added or deleted, so the operation is very small, and the speed is very fast. The unshift (shift) is added to the first element, because the address points to which the array points are not required, then need Move a position behind all the elements, this, the larger the array, the lower the efficiency, and the first exact opposite, you need to move the rear elements to move forward.

转载请注明原文地址:https://www.9cbs.com/read-63698.html

New Post(0)