Last active
June 10, 2016 10:00
-
-
Save masahitojp/475f8da57fd54f56bf9d7786c1311f6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'java' | |
sourceCompatibility = 1.8 | |
targetCompatibility = 1.8 | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
compile 'org.eclipse.collections:eclipse-collections:7.1.0' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.eclipse.collections.api.list.primitive.MutableIntList; | |
import org.eclipse.collections.impl.factory.primitive.IntIntMaps; | |
import org.eclipse.collections.impl.factory.primitive.IntLists; | |
import java.util.Random; | |
import java.util.function.Supplier; | |
import java.util.stream.IntStream; | |
public class Main { | |
private static <T> void time(final String name, Supplier<T> func) { | |
long s = System.nanoTime(); | |
func.get(); | |
System.out.println(String.format("%s: %d elapsed.", name, (System.nanoTime() - s) / 1_000_000)); | |
} | |
public static void main(String[] args) { | |
final MutableIntList source = IntLists.mutable.empty(); | |
final Random r = new Random(); | |
IntStream.range(0, 50_000_000).forEach(i -> source.add(r.nextInt())); | |
time("Insertion", () -> source.injectInto(IntIntMaps.mutable.empty(), (x, y) -> { | |
x.put(y, 0); | |
return x; | |
})); | |
time("Lookups", () -> source.injectInto(0, (x, y) -> x + (y % 10))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Random; | |
import java.util.function.Supplier; | |
public class Main { | |
private static <T> void time(final String name, Supplier<T> func) { | |
long start = System.nanoTime(); | |
func.get(); | |
System.out.println(String.format("%s : %d elapsed.", name, (System.nanoTime() - start) / 1000000 )); | |
} | |
public static void main(String[] args) { | |
final int j = 50_000_000; | |
final int[] source = new int[j]; | |
final Random r = new Random(); | |
for (int i = 0; i < j ; i++) { | |
source[i] = r.nextInt(); | |
} | |
//final HashMap<Integer, Integer> m = new HashMap<>(j); | |
final MutableIntIntMap m = IntIntMaps.mutable.empty(); | |
time("Insertion", () -> { | |
for (int i = 0; i < j ; i++) { | |
m.put(i, 0); | |
} | |
return j; | |
}); | |
time("Lookups", () -> { | |
long acc = 0L; | |
for (int i = 0; i < j ; i++) { | |
acc += source[i] % 10; | |
} | |
return acc; | |
}); | |
} | |
} |
https://gist.github.com/zakki/66d785b4a9d3c64bc9f8199d7e290e07
を参考にeclipse collectionを使ってみる
// eclipse collectionを使用
Insertion: 1040 elapsed.
Lookups: 111 elapsed.
// ECMain.javaの方の結果
Insertion: 4793 elapsed.
Lookups: 482 elapsed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ java -version
java version "1.8.0_73"
Java(TM) SE Runtime Environment (build 1.8.0_73-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)
$ java -server Main
Insertion : 42535 elapsed.
Lookups : 171 elapsed.