我会让每个线程处理自己的地图。这意味着每个线程可以相互依赖地工作。线程完成后,您可以合并所有结果。(或者可能在完成时将结果组合起来,但这可能会增加复杂性而没有太大优势)
如果您使用的是短的,我会在像TObjectIntHashMap这样的集合中使用它,它对于处理原语更有效。
在简单的情况下,您有short
坐标 public static void main(String... args) throws IOException { int length = 10 * 1000 * 1000; 整数 [] x = 新整数 [长度]; 整数 [] y = 新整数 [长度];
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>