English 中文(简体)
尽快在贾瓦古斯塔签署32个集束阵列?
原标题:Fastest way to sort 32bit signed integer arrays in JavaScript?
_radixSort_0 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
/*
RADIX SORT
Use 256 bins
Use shadow array
- Get counts
- Transform counts to pointers
- Sort from LSB - MSB
*/
function radixSort(intArr) {
    var cpy = new Int32Array(intArr.length);
    var c4 = [].concat(_radixSort_0); 
    var c3 = [].concat(_radixSort_0); 
    var c2 = [].concat(_radixSort_0);
    var c1 = [].concat(_radixSort_0); 
    var o4 = 0; var t4;
    var o3 = 0; var t3;
    var o2 = 0; var t2;
    var o1 = 0; var t1;
    var x;
    for(x=0; x<intArr.length; x++) {
        t4 = intArr[x] & 0xFF;
        t3 = (intArr[x] >> 8) & 0xFF;
        t2 = (intArr[x] >> 16) & 0xFF;
        t1 = (intArr[x] >> 24) & 0xFF ^ 0x80;
        c4[t4]++;
        c3[t3]++;
        c2[t2]++;
        c1[t1]++;
    }
    for (x=0; x<256; x++) {
        t4 = o4 + c4[x];
        t3 = o3 + c3[x];
        t2 = o2 + c2[x];
        t1 = o1 + c1[x];
        c4[x] = o4;
        c3[x] = o3;
        c2[x] = o2;
        c1[x] = o1;
        o4 = t4;
        o3 = t3;
        o2 = t2;
        o1 = t1;
    }
    for(x=0; x<intArr.length; x++) {
        t4 = intArr[x] & 0xFF;
        cpy[c4[t4]] = intArr[x];
        c4[t4]++;
    }
    for(x=0; x<intArr.length; x++) {
        t3 = (cpy[x] >> 8) & 0xFF;
        intArr[c3[t3]] = cpy[x];
        c3[t3]++;
    }
    for(x=0; x<intArr.length; x++) {
        t2 = (intArr[x] >> 16) & 0xFF;
        cpy[c2[t2]] = intArr[x];
        c2[t2]++;
    }
    for(x=0; x<intArr.length; x++) {
        t1 = (cpy[x] >> 24) & 0xFF ^ 0x80;
        intArr[c1[t1]] = cpy[x];
        c1[t1]++;
    }
    return intArr;
}

EDIT:

So far, the best/only major optimization brought to light is JS typed arrays. Using a typed array for the normal radix sort s shadow array has yielded the best results. I was also able to squeeze a little extra out of the in place quick sort using JS built in stack push/pop.


http://jsfiddle.net/u8t2a/61/”rel=“nofollow noreferer”>

Intel i7 870, 4GB, FireFox 8.0
2mil
radixSort(intArr): 172 ms
radixSortIP(intArr): 1738 ms
quickSortIP(arr): 661 ms
200k
radixSort(intArr): 18 ms
radixSortIP(intArr): 26 ms
quickSortIP(arr): 58 ms

www.un.org/Depts/DGACM/index_spanish.htm 看来标准rad是这一工作流程的国王。 如果有人有时间尝试整顿或作其他修改,我将不胜感激。

我有一个具体使用案例,即我喜欢在 Java文中最快的分类执行工作。 将有大量(50 000 - 2mil)、未经许可(基本上随机)、分类(32个已签名)的阵列,客户的文字可以查阅,然后需要分类并提供这一数据。

I ve实施了较为快速的射线,并安装了快速的jsfiddlebase,但对于我的高压阵列来说,它们仍然相当缓慢。 快速度在我的高约束阵列规模上表现得较好,而rad则在我的下限上表现得较好。

defaultSort is the built-in JavaScript array.sort with an integer compare function

Intel C2Q 9650, 4GB, FireFox 3.6
2mil
radixSortIP(intArr): 5554 ms
quickSortIP(arr): 1796 ms
200k
radixSortIP(intArr): 139 ms
quickSortIP(arr): 190 ms
defaultSort(intArr): 354 ms

Intel i7 870, 4GB, FireFox 8.0
2mil
radixSortIP(intArr): 990 ms
quickSortIP(arr): 882 ms
defaultSort(intArr): 3632 ms
200k
radixSortIP(intArr): 28 ms
quickSortIP(arr): 68 ms
defaultSort(intArr): 306 ms

Questions

  • Is there a better implementation of any sorting algorithm that would meet my use case/needs?
  • Are there any optimizations that can be made to my in place radix/quick sort implementations to improve performance?
    • Is there an efficient way to convert my in place radix sort from a recursive to iterative function? Memory and execution speed.

Goal

  • I am hoping these answers will help me get ~20-30% performance improvement in my benchmark test.

Clarifications/Notes

  • "DEFINE FAST" I would prefer a general case where it runs well on all modern browsers, but if there is a browser specific optimization that makes a significant improvement that may be acceptable.
  • The sorting COULD be done server side, but I d prefer to avoid this because the JS app may become a standalone (paired with some off the shelf proprietary app that will stream sensor data to a file).
  • JavaScript may not be the best language for this but it s a requirement.
  • I ve already asked this question https://stackoverflow.com/questions/7111525/fastest-way-to-sort-integer-arrays-in-javascript an incorrect answer was up-voted and the question was closed.
  • I ve attempted using multiple browser window instances as a makeshift multi-threading; it didn t pan out. I d be interested in useful info regarding spawning multiple windows for concurrency.
最佳回答
问题回答

您是否考虑将各种算法结合起来,以尽量利用海滩? 我在基准中看到,当亚拉伊斯小时,你正在转向插入。 一种有趣的做法,是转向蒸.。 在快速使用的情况下,可以将最坏的案件降为O(nlog(n)而不是O(n^2)。 查阅Introsort

I fiddled with your benchmark and added my own sort function. It performs same as radixsort, but it s idea (and implementation) is simpler, it is like a radixsort, but in binary, so you only have two bucket and can do it in-place. Look at http://jsfiddle.net/KaRV9/7/.

I put my code in place of "Quicksort in place" (since it is very similar to quicksort, just pivot is selected other way). Run them a few times, in my Chrome 15 they perform so close it is unable to distinguish them.

我不会就你的分类算法发表意见。 你们比我更了解这些问题。

但一个好的想法是使用网络工人。 这使得你能够亲眼看管,从而不会阻挡接口。 不管怎样,这都是好的做法。 它对 Chrome和 Firefox。 操作的版本不读。 不清楚EE的支持,但很容易围绕这一点开展工作。 当然,使用多线透镜涉及间接费用。

雇佣军可以很容易地进入多读版本,这将使你能够提高业绩。 当然,如果能够更快地运行,就确实取决于你的具体情况。 但是,考虑到非锁定性质可能会使其感到像对终端用户来说,这种说法正在更快地运作。

EDIT:我看到你已经用插入类型替代较小的子宫。 我忽略了这一点。

迅速采取的真正世界办法就是检查亚拉底的面积,如果它使用速效低头的速效办法,如插入型,则过于缓慢。

Pseudo-code与下列内容大致相同:

quicksort(array, start, end):
  if ( (end-start) < THRESHOLD ) {
    insertion_sort(array, start, end);
    return;
  }
  // regular quicksort here
  // ...

为了确定住房条件,你需要将其放在你所关心的平台上,如果是的话,则可能是不同的浏览器。 衡量不同门槛值的随机阵列寻找接近最佳状态的时间。 如果发现不同的浏览器,你也可以选择不同的门槛值。

现在,如果你的投入确实是随机的(很常见),你就可以看到,更好的假肢选择会改善业绩。 一种共同的方法是:http://en.wikipedia.org/wiki/Quicksort#Choice_of_pivot” rel=“nofollow” 。





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...