我會讓每個線程處理自己的Map。這意味着每個線程可以相互依賴地工作。一旦線程完成,您可以將所有結果合併。 (或可能結合完成後的結果,但這可能會增加複雜性,但沒有多大優勢)
如果您使用的是short,我將使用像TObjectIntHashMap這樣的集合,這對於處理基元更有效。
在簡單的情況下,必須short
座標 公共靜態無效主要(字符串...參數)拋出IOException異常{ INT長度= 10 * 1000 * 1000; int [] x = new int [length]; int [] y = new int [length];
Random rand = new Random();
for (int i = 0; i < length; i++) {
x[i] = rand.nextInt(10000) - rand.nextInt(10000);
y[i] = rand.nextInt(10000) - rand.nextInt(10000);
}
countPointsWithLongIntMap(x, y);
countPointsWithMap(x, y);
}
private static Map<String, Short> countPointsWithMap(int[] x, int[] y) {
long start = System.nanoTime();
Map<String, Short> counts = new LinkedHashMap<String, Short>();
for (int i = 0; i < x.length; i++) {
String key = x[i] + "," + y[i];
Short s = counts.get(key);
if (s == null)
counts.put(key, (short) 1);
else
counts.put(key, (short) (s + 1));
}
long time = System.nanoTime() - start;
System.out.printf("Took %.3f seconds to use Map<String, Short>%n", time/1e9);
return counts;
}
private static TIntIntHashMap countPointsWithLongIntMap(int[] x, int[] y) {
long start = System.nanoTime();
TIntIntHashMap counts = new TIntIntHashMap();
for (int i = 0; i < x.length; i++) {
int key = (x[i] << 16) | (y[i] & 0xFFFF);
counts.adjustOrPutValue(key, 1, 1);
}
long time = System.nanoTime() - start;
System.out.printf("Took %.3f seconds to use TIntIntHashMap%n", time/1e9);
return counts;
}
打印
Took 1.592 seconds to use TIntIntHashMap
Took 4.889 seconds to use Map<String, Short>
如果你有雙座標,你需要使用一個兩層的地圖。
public static void main(String... args) throws IOException {
int length = 10 * 1000 * 1000;
double[] x = new double[length];
double[] y = new double[length];
Random rand = new Random();
for (int i = 0; i < length; i++) {
x[i] = (rand.nextInt(10000) - rand.nextInt(10000))/1e4;
y[i] = (rand.nextInt(10000) - rand.nextInt(10000))/1e4;
}
countPointsWithLongIntMap(x, y);
countPointsWithMap(x, y);
}
private static Map<String, Short> countPointsWithMap(double[] x, double[] y) {
long start = System.nanoTime();
Map<String, Short> counts = new LinkedHashMap<String, Short>();
for (int i = 0; i < x.length; i++) {
String key = x[i] + "," + y[i];
Short s = counts.get(key);
if (s == null)
counts.put(key, (short) 1);
else
counts.put(key, (short) (s + 1));
}
long time = System.nanoTime() - start;
System.out.printf("Took %.3f seconds to use Map<String, Short>%n", time/1e9);
return counts;
}
private static TDoubleObjectHashMap<TDoubleIntHashMap> countPointsWithLongIntMap(double[] x, double[] y) {
long start = System.nanoTime();
TDoubleObjectHashMap<TDoubleIntHashMap> counts = new TDoubleObjectHashMap<TDoubleIntHashMap>();
for (int i = 0; i < x.length; i++) {
TDoubleIntHashMap map = counts.get(x[i]);
if (map == null)
counts.put(x[i], map = new TDoubleIntHashMap());
map.adjustOrPutValue(y[i], 1, 1);
}
long time = System.nanoTime() - start;
System.out.printf("Took %.3f seconds to use TDoubleObjectHashMap<TDoubleIntHashMap>%n", time/1e9);
return counts;
}
打印
Took 3.023 seconds to use TDoubleObjectHashMap<TDoubleIntHashMap>
Took 7.970 seconds to use Map<String, Short>
很好,謝謝你的建議。我現在將執行此操作並回復給您。我想我最初以爲在試圖將所有的HashMaps結合在一起時你會做同樣的工作,但是在處理線程時它可能會更好,這對我來說並不專業。 –
操作次數相同,但每個CPU都有自己的緩存,本地Map可以使用該緩存。如果你有一個共享映射,它將在最慢的緩存中(最好) –
我還會看到你是否可以避免使用String作爲關鍵字,因爲這可能比使用long和TLongIntHashMap長10-100倍。 –