奇怪的表现怪癖javascript

时间:2016-07-22 17:12:37

标签: javascript arrays node.js performance sorting

根据我正在使用的数组,我在简单任务的性能方面遇到了奇怪的差异。任务是计算数组中大于5的元素之和。任务在具有相等长度的数组上执行。

我在三个不同的数组对象上尝试相同的方法:

1)var hugeArray1 - 从0:10范围随机挑选所有elemenet的数组

2)var hugeArray2 - 使用Array.prototype.sort()

排序的hugeArray1的副本

3)var hugeArray3 - 手工制作但排序的数组,其值范围为0:10,扩展到等于这个间隔。

我尝试为每个数组计算大于5次的元素总和,然后对它们求平均值。奇怪的是,这三个阵列所需的时间差异很大。

1)hugeArray1:5.805ms

2)hugeArray2:15.738ms

3)hugeArray3:3.753ms

使用sort()排序的数组的结果非常差。这是为什么?它看起来像sort()返回某种包装器/代理而不是' native'数组,影响性能。我试过两台电脑。我也试图改变测试顺序。

我在下面提供了代码,请告诉我这里发生了什么。

// random array with elements 0-10 of size n    
function randomArray(n) {
    var arr = [];
    for (var i = 0; i < n; ++i) {
        arr.push(Math.random() * 10);
    }
    return arr;
};

// measures time of execution
function measureTime(f) {
    var start = new Date().getTime();
    f();
    var stop = new Date().getTime();
    return stop - start;

};

// enumerate ofer array and calculate sum of elementsgreater than 5
function sumGreaterThan5(arr) {
    var sum = 0;
    for (var i = 0; i < arr.length; ++i) {
        if (arr[i] > 5.0)
            sum += arr[i];
    }
    return sum;
}

// generate array os size 'size' with elements with constant step to fill interval 0:10
function generateSortedArr(size) {
    var arr = [];
    for (var i = 0; i < size; ++i) {
        arr.push(i * 10 / size);
    }
    return arr;
}

var huge = 1000000;
var hugeArray1 = randomArray(huge);
var hugeArray2 = hugeArray1.slice(0).sort();
var hugeArray3 = generateSortedArr(huge);

var hugeArrays = [hugeArray1, hugeArray2, hugeArray3];

hugeArrays.forEach(x=> {
    var res = [];
    for (var i = 0; i < 1000; ++i) {
        res.push(measureTime(function () {
            sumGreaterThan5(x);
        }));
    }
    console.log(res.reduce((prev, curr)=> prev + curr) / res.length);
});

&#13;
&#13;
// random array with elements 0-10 of size n    
function randomArray(n) {
    var arr = [];
    for (var i = 0; i < n; ++i) {
        arr.push(Math.random() * 10);
    }
    return arr;
};

// measures time of execution
function measureTime(f) {
    var start = new Date().getTime();
    f();
    var stop = new Date().getTime();
    return stop - start;

};

// enumerate ofer array and calculate sum of elementsgreater than 5
function sumGreaterThan5(arr) {
    var sum = 0;
    for (var i = 0; i < arr.length; ++i) {
        if (arr[i] > 5.0)
            sum += arr[i];
    }
    return sum;
}

// generate array os size 'size' with elements with constant step to fill interval 0:10
function generateSortedArr(size) {
    var arr = [];
    for (var i = 0; i < size; ++i) {
        arr.push(i * 10 / size);
    }
    return arr;
}

var huge = 1000000;
var hugeArray1 = randomArray(huge);
var hugeArray2 = hugeArray1.slice(0).sort();
var hugeArray3 = generateSortedArr(huge);

var hugeArrays = [hugeArray1, hugeArray2, hugeArray3];


hugeArrays.forEach(function(x){
    var res = [];
    for (var i = 0; i < 1000; ++i) {
        res.push(measureTime(function () {
            sumGreaterThan5(x);
        }));
    }
    console.log(res.reduce(function(prev, curr){return prev + curr},0) / res.length);
});
&#13;
&#13;
&#13;

0 个答案:

没有答案