什么是重新排序数组的最快方法 [英] What is the Fastest way to reorder an array
问题描述
什么是最快的(实时数据处理应用程序)的方式来重新排序(新的指数都是一样的),Java中的数组:
What is the fastest (realtime data processing application) way to reorder (the new indices are always the same) an array in JAVA:
例如为:
我有:
双击[] A =新的双[] {1,234,12,99,0};
我需要得到迅速:
双击[] B =新的双[] {A [2],A [4],A [0],A [1],A [3]};
但是,也许这是这是最有效的方式做到这一点呢?
But maybe this is this is the most efficient way to do it anyway?
非常感谢您的反馈意见
推荐答案
我怀疑你能比你目前的做法做的更好。
I doubt you can do better than your current approach of
double[] B = new double[] {A[2], A[4], A[0], A[1], A[3]};
对于其他序列可能的候选人可能是 Arrays.copyOf
或 Arrays.copyOfRange
的形式,但最低金额工作必须在这里做的包括:
Likely candidates for other sequences might be forms of Arrays.copyOf
or Arrays.copyOfRange
, but the minimum amount of work you must do here consists of:
- 创建一个新的数组
- 随机访问每个元件阵列中
有一个小的机会,你可能会很具体的阅读稍微好一点的做/写命令(以充分利用高速缓存行),一个猜测是什么,才能完全读取并以升序写道几乎全部:
There's a small chance that you might do slightly better with very specific read/write orders (to take advantage of cache lines), one guess is something that reads entirely in order and writes almost entire in ascending order:
double[] B = new double[A.length];
B[2] = A[0];
B[3] = A[1];
B[4] = A[3];
B[0] = A[2];
B[1] = A[4];
但我没有这个是明显好强的预期。如果你在那里你试图消除或优化的L1 / L2缓存命中点,是时候开始微标,而真正的答案是你应该尝试。
But I don't have strong expectations of this being noticeably better. If you're at the point where you're trying to eliminate or optimize on L1/L2 cache hits, it's time to start micro-benchmarking, and the real answer is you should experiment.
这篇关于什么是重新排序数组的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!