3

In Java, I have:

Set<Integer> set = new HashSet<Integer>();
callVoidMethod(set);
...
public static void callVoidMethod(Set<Integer> set) {

    Set<Integer> superset = new HashSet<Integer>(set);
    ...
    // I just added this loop to show that I'm adding quite a lot
    // well, it depends on conditions, sometimes I add nothing,
    // but it is unpredictable and do not know if add something
    for (int i = 0; i < 1000; i++) {
         ...
         if (conditionSatisfied) superset.add(someValue);
         ...
    }

}

The code above is simplified, the idea is to pass the set by reference into a void method and create a full copy of a set such that we will be able to add some new elements to the copy (superset here) and do not touch the set as we need it untouched when we exit the void method.

My code works with lots of data processing and if there is no faster way to make a copy, then I would like to optimize the HashSet itself, for instance I do not need Integers as keys, but better primitive ints. Would be a good idea to implement an int[] array of keys in the MyHashSet?

If so is possible, I would be interested in using the same idea for improving this:

Map<Integer, ArrayList<Item>> map = new HashMap<Integer, ArrayList<Item>>();

EDIT: I need only speed-performance-optimization. I do not need beautiful-maintainable code and memory.

4

3 回答 3

8

In general, if you're looking for high speed collections that allow for primitives, consider using Trove. I would say - don't optimize unless you've discovered that this is actually a bottleneck. You or someone else will need to maintain this code, and reading an optimized version is often harder.

于 2012-08-06T16:58:46.690 回答
6

Have you tried first tweaking the HashSet's initial capacity and load factor?

HashSet

Here's a post that might help you.

HashMap initialization parameters

If you have such a big amount of data to process, it would probably payoff to analyze it's distribution and adjust these settings first.

Having tweaked that, it might give a very slight performance to replace Integers with ints, but it might depend more on JVM implementation specifics and hardware configuration than what this improvement alone would give you.

于 2012-08-06T17:03:03.340 回答
5

What do you do with these objects later? If you're just doing lookups or something like that, it might be faster to keep them separate and check both, rather than making a full copy. So,

public static void callVoidMethod(Set<Integer> set) {

    Set<Integer> superset = new HashSet<Integer>();
    ...
    if (conditionSatisfied) superset.add(someValue);

    ...
    if(set.contains(value) || superset.contains(value))
        doSomething();

}
于 2012-08-06T17:00:40.460 回答