拼接比Shift + Pop快吗?为什么? [英] Is Splice faster than Shift + Pop? Why?

查看:133
本文介绍了拼接比Shift + Pop快吗?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一些非特定来源听说,结合使用shift()pop()比使用splice()更快.我尚未进行基准测试,而且由于我不在城市,很可能一段时间都不会进行基准测试,但这是真的,如果是这样,为什么?

解决方案

如果不需要保留数组的顺序,则使用.pop()比使用.splice()要快得多.

.splice()将删除给定索引处的元素.然后发生的事情是,此数组之后的每个其他元素都需要将其在数组中的位置减少1.如果数组很大而删除元素的索引很小,则这可能会很慢.

使用.pop(),您可以删除完全重新索引所有内容的此过程.您可以使用.pop()从数组中删除最后一个项目,而不是删除给定索引处的元素.从这里开始,您要做的就是将要删除的项目替换为使用.pop()获得的项目.如前所述,这当然意味着将不保留元素的顺序.

代码示例:

.splice():

var list:Array = [0,1,2,3,4,5];
list.splice(2, 1);

trace(list); // 0,1,3,4,5

然后是.pop():

var last:* = list.pop();
list[2] = last;

trace(list); // 0,1,5,4 - Notice we move 5 to the index 2 and lose the order.

这是最重要的实际性能测试:

var list:Array = [];
function fillList():void
{
    for(var i = 0; i < 200000; i++) list.push(i);
}


function emptyViaSplice():void
{
    fillList();
    var t:Number = getTimer();

    while(list.length > 0)
    {
        list.splice(0, 1);
    }

    trace("emptyViaSplice: " + (getTimer() - t));
}


function emptyViaPop():void
{
    fillList();
    var t:Number = getTimer();

    while(list.length > 0)
    {
        if(list.length == 1) list.pop();
        else
        {
            var l:* = list.pop();
            list[0] = l;
        }
    }

    trace("emptyViaPop: " + (getTimer() - t));
}

结果:

emptyViaSplice(); // 12153 ms
emptyViaPop(); // 37 ms

I've heard from some nonspecific sources that using a combination of shift() and pop() is faster than using splice(). I haven't run a benchmark test yet and will most likely not for a while since I'm out of town, but is this true, and if so, why?

解决方案

If you don't need to retain the order of your Array, using .pop() is much, much faster than .splice().

.splice() will remove an element at a given index. What happens then is that every other element in the array after that one will need to have its position in the array reduced by 1. This can be slow if your array is large and the index at which you remove an element is small.

Using .pop(), you can remove this process of re-indexing everything entirely. Instead of removing an element at a given index, you can use .pop() to remove the last item from the array. From here, all you need to do is replace the item you want to remove with the one you got from using .pop(). As mentioned, this will of course mean that the order of your elements is not maintained.

Code examples:

.splice():

var list:Array = [0,1,2,3,4,5];
list.splice(2, 1);

trace(list); // 0,1,3,4,5

And then .pop():

var last:* = list.pop();
list[2] = last;

trace(list); // 0,1,5,4 - Notice we move 5 to the index 2 and lose the order.

And here we have the all-important actual performance tests:

var list:Array = [];
function fillList():void
{
    for(var i = 0; i < 200000; i++) list.push(i);
}


function emptyViaSplice():void
{
    fillList();
    var t:Number = getTimer();

    while(list.length > 0)
    {
        list.splice(0, 1);
    }

    trace("emptyViaSplice: " + (getTimer() - t));
}


function emptyViaPop():void
{
    fillList();
    var t:Number = getTimer();

    while(list.length > 0)
    {
        if(list.length == 1) list.pop();
        else
        {
            var l:* = list.pop();
            list[0] = l;
        }
    }

    trace("emptyViaPop: " + (getTimer() - t));
}

The results:

emptyViaSplice(); // 12153 ms
emptyViaPop(); // 37 ms

这篇关于拼接比Shift + Pop快吗?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆