我必须设计一个接口方法,它接受primitive []数组,执行排序并返回primitive []数组,以满足低延迟(高性能)要求,并将被多个线程同时调用
对于这个需要很高性能的目的,排序的set和int[]哪个更好?
感谢您的回复,谢谢
发布于 2013-05-18 00:23:48
这个方法可能会被调用,比方说,每秒200万次,我确信Array.Sort在这方面效率不高。此数组中的最大大小可以为100个元素
一个快速的微基准测试表明,Arrays.sort可以对一个包含100个int的int[]数组进行排序。1.3微秒*,在标准台式机(i7)上,使用一个内核。
因此,您可以每秒调用大约800,000次(仍然假设您只使用一个内核)。因此,如果你有4个或更多的处理器,你应该能够每秒运行200万次排序操作。
注意:如果您的数组有一个典型的特征(比如有很多重复项,或者大部分是排序的,或者数字都在一个相当小的范围内),那么您可能能够找到一个更适合的算法,但是对于一般的用例,我非常确定JDK算法是相当健壮和高效的。
*微基准的结果(使用jmh完成):
Run result "sort": 1341.298 ±(95%) 11.701 ±(99%) 19.406 nsec/op
Run statistics "sort": min = 1331.329, avg = 1341.298, max = 1352.831, stdev = 9.425
Run confidence intervals "sort": 95% [1329.597, 1352.999], 99% [1321.892, 1360.704]发布于 2013-05-17 23:40:39
试试这个。
String[] fruits = new String[] {"Pineapple","Apple", "Orange", "Banana"};
Arrays.sort(fruits);
int i=0;
for(String temp: fruits){
System.out.println("fruits " + ++i + " : " + temp);
}或者这个..。
List<String> fruits = new ArrayList<String>();
fruits.add("Pineapple");
fruits.add("Apple");
fruits.add("Orange");
fruits.add("Banana");
Collections.sort(fruits);
int i=0;
for(String temp: fruits){
System.out.println("fruits " + ++i + " : " + temp);
}看看这个。QuickSort
https://stackoverflow.com/questions/16612869
复制相似问题