开发者

Custom sorting function bottleneck

I am trying to sort big array using actionscript 3.

The problem is that i have to use custom sorting function which is painfully slow and leads to flash plugin crash.

Below is a sample code for custom function used to sort array by length of its members:

            private function sortByLength():int {
                var x:int = arguments[0].length;
                var y:int = arguments[1].length;
                if (x > y){
                    return 1;                       
                }else if (x < y){
                    return -1;
                }else{
                    return 0;
                }
             }

Which is called like this:

    var txt:Array = ["abcde","ab","abc","a"];
    txt.sort(sortByLength);

Please advise me how can this be done faster ?

How to change application logic to avoid Flash plugin cra开发者_开发问答shes during sorting ?


try to use strong typing whenever possible, here tell your function that you are waiting two strings.

you could rewrite your function in two way one fastest than the other if you know that all your element are not null:

function sortByLength(a:String, b:String):int {
    return a.length-b.length // fastest way not comparison
}

and if you can have null check for it (this one will put null in front of all element):

 function sortByLengthWithNull(a:String, b:String):int {
     if (a==null) return -1
     if (b==null) return 1
     return a.length-b.length
 }


If you need super-fast sorting, then it might be worthwhile not using an array at all and instead using a linked-list. There are different advantages to each. Primarily, with a linked-list, index-access is slow, while iterating through the list is fast, and linked-lists are not native to AS3 so you'll have to roll your own.

On the upside, you may well be able to use some of Polygonal Labs' code: http://lab.polygonal.de/as3ds/.

Sorting is very, very fast for nearly-sorted data with a linked list, as this article discusses: http://lab.polygonal.de/2007/11/26/data-structures-more-on-linked-lists/.

This solution gives you lots more work, but will eventually give you lots more sort-speed too.

Hope this helps.

-- additional --

I noticed your question in the comments of another answer about "One question however is unanswered - how to perform greedy computations in Flash without hanging it?"

For this, essentially the answer is to break your computation over multiple frames, something like this:

public function sort():void
{
    addEventListener(Event.ENTER_FRAME, iterateSort);
}

private function iterateSort():void
{
    var time:int = getTimer() + TARGET_MILLISECONDS_PER_FRAME;
    var isFinished:Boolean = false;

    while (!isFinished && getTimer() < time)
        isFinished = continueSort();

    if (isFinished)
        removeEventListener(Event.ENTER_FRAME, iterateSort);
}

function continueSort():Boolean
{
    ... implement an 'atom of sort' here, whatever that means ...
}


sortByLength should have two parameters, shouldn't it? I guess that's what you mean by the arguments array...

This looks fine to me, unless arguments is not a local variable, but instead a member variable, and you're just looking at its [0] and [1] elements on each function call. That would at least produce undesired results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜