在 Actionscript 中连接数组的最佳方法

发布于 2024-10-12 02:18:45 字数 559 浏览 3 评论 0原文

我需要将一个数组添加到另一个数组(只关心保存连接的数组)。哪种方法是首选?速度是首要考虑因素,其次是可读性(我认为选项 1 是一个更简洁的选项)。我认为它也可能取决于数组的长度,但是有什么好的指导方针吗?

选项 1:

var array1:Array = new Array("1","2","3");
var array2:Array = new Array("4","5","6");

// Don't care about array2 after this point.
var array1 = array1.concat(array2);

选项 2:

var array1:Array = new Array("1","2","3");
var array2:Array = new Array("4","5","6");

// Don't care about array2 after this loop has run.
for each(var item:Object in array2)
{
    array1.push(item);
}

I need to add one array to another (only care about saving the joined one). Which is the preferred way of doing this? Speed is the primary concern followed by readability (I consider Option 1 to be a cleaner option). I assume it might also depend on the length of the arrays, but are there any good guidelines?

Option 1:

var array1:Array = new Array("1","2","3");
var array2:Array = new Array("4","5","6");

// Don't care about array2 after this point.
var array1 = array1.concat(array2);

Option 2:

var array1:Array = new Array("1","2","3");
var array2:Array = new Array("4","5","6");

// Don't care about array2 after this loop has run.
for each(var item:Object in array2)
{
    array1.push(item);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕嗳→ 2024-10-19 02:18:45

这听起来像是一项......基准测试的工作!

import flash.utils.getTimer;

function addItems($array:Array, $start:int, $count:int) {
    for (var i:Number = $start; i < $start + $count; i++) {
        $array.push(i);
    }
}

function concatArrays($array1:Array, $array2:Array):Number {
    var t1:Number = getTimer();
    $array1.concat($array2);
    var t2:Number = getTimer();
    return t2 - t1;
}

function pushArrays($array1:Array, $array2:Array):Number {
    var t1:Number = getTimer();
    for each (var item:Object in $array2) {
        $array1.push(item);
    }
    var t2:Number = getTimer();
    return t2 - t1;
}

function testBed() {
    for (var i:Number = 10000; i <= 100000; i+=10000) {
        trace("\n---- New test");
        var a1:Array = [];
        var a2:Array = [];
        addItems(a1, 0, i);
        addItems(a2, i, i);
        trace("For " + a1.length + " items: ");
        trace("concatArrays: " + concatArrays(a1, a2));
        trace("pushArrays:   " + pushArrays(a1, a2));
    }
}

testBed();

正如我所怀疑的, concat 速度要快得多,尤其是在处理较大的数组时。

输出

---- New test
For 10000 items: 
concatArrays: 1
pushArrays:   1

---- New test
For 20000 items: 
concatArrays: 1
pushArrays:   4

---- New test
For 30000 items: 
concatArrays: 1
pushArrays:   4

---- New test
For 40000 items: 
concatArrays: 2
pushArrays:   5

---- New test
For 50000 items: 
concatArrays: 1
pushArrays:   6

---- New test
For 60000 items: 
concatArrays: 1
pushArrays:   7

---- New test
For 70000 items: 
concatArrays: 1
pushArrays:   8

---- New test
For 80000 items: 
concatArrays: 2
pushArrays:   12

---- New test
For 90000 items: 
concatArrays: 2
pushArrays:   13

---- New test
For 100000 items: 
concatArrays: 3
pushArrays:   14

这些数字以毫秒为单位,因此正如 Richard 指出的那样,除非您的数组具有大量元素,或者您非常频繁地连接数组,否则这种优化不值得您时间

This sounds like a job for... benchmark!

import flash.utils.getTimer;

function addItems($array:Array, $start:int, $count:int) {
    for (var i:Number = $start; i < $start + $count; i++) {
        $array.push(i);
    }
}

function concatArrays($array1:Array, $array2:Array):Number {
    var t1:Number = getTimer();
    $array1.concat($array2);
    var t2:Number = getTimer();
    return t2 - t1;
}

function pushArrays($array1:Array, $array2:Array):Number {
    var t1:Number = getTimer();
    for each (var item:Object in $array2) {
        $array1.push(item);
    }
    var t2:Number = getTimer();
    return t2 - t1;
}

function testBed() {
    for (var i:Number = 10000; i <= 100000; i+=10000) {
        trace("\n---- New test");
        var a1:Array = [];
        var a2:Array = [];
        addItems(a1, 0, i);
        addItems(a2, i, i);
        trace("For " + a1.length + " items: ");
        trace("concatArrays: " + concatArrays(a1, a2));
        trace("pushArrays:   " + pushArrays(a1, a2));
    }
}

testBed();

As I would have suspected, concat is far faster, especially when dealing with larger arrays.

OUTPUT

---- New test
For 10000 items: 
concatArrays: 1
pushArrays:   1

---- New test
For 20000 items: 
concatArrays: 1
pushArrays:   4

---- New test
For 30000 items: 
concatArrays: 1
pushArrays:   4

---- New test
For 40000 items: 
concatArrays: 2
pushArrays:   5

---- New test
For 50000 items: 
concatArrays: 1
pushArrays:   6

---- New test
For 60000 items: 
concatArrays: 1
pushArrays:   7

---- New test
For 70000 items: 
concatArrays: 1
pushArrays:   8

---- New test
For 80000 items: 
concatArrays: 2
pushArrays:   12

---- New test
For 90000 items: 
concatArrays: 2
pushArrays:   13

---- New test
For 100000 items: 
concatArrays: 3
pushArrays:   14

Those numbers are in milliseconds, so as Richard points out, unless your arrays have a huge number of elements, or you are concatenating arrays very frequently, then this is an optimization that is not worth your time

九八野马 2024-10-19 02:18:45

我个人会使用 concat,因为它更简单。

它可能也更快(因为它可以本地实现),但如果它对您很重要,请衡量它。除非您正在处理大量的数组值,否则不太可能有差异,并且这将是微优化的一个主要示例。

I would use concat personally, because it's simpler.

It's probably faster, too (because it can be natively implemented), but measure it if it's important to you. Unless you are dealing with an extremely large number of array values, there's unlikely to be a difference and would be a prime example of micro-optimisation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文