Created
September 5, 2014 11:21
-
-
Save skrb/3f7125e42ed015275287 to your computer and use it in GitHub Desktop.
StreamとパラレルStreamの比較
This file contains hidden or 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.function.IntConsumer; | |
import java.util.stream.IntStream; | |
public class Sum { | |
private static final int SIZE = 1_000_000; | |
public Sum() { | |
evaluate("sum000", i -> sum000()); | |
evaluate("sum001", i -> sum001()); | |
evaluate("sum002", i -> sum002()); | |
} | |
private void evaluate(String prefix, IntConsumer consumer) { | |
IntStream.range(0, 20) | |
.forEach(consumer); | |
long start = System.nanoTime(); | |
IntStream.range(0, 100) | |
.forEach(consumer); | |
long end = System.nanoTime(); | |
result(prefix, end - start); | |
} | |
private IntStream createIntStream() { | |
return IntStream.range(1, SIZE); | |
} | |
private int sum000() { | |
int sum = 0; | |
for (int i = 0; i <= SIZE; i++) { | |
sum += i; | |
} | |
return sum; | |
} | |
private int sum001() { | |
return createIntStream().sum(); | |
} | |
private int sum002() { | |
return createIntStream().parallel().sum(); | |
} | |
private void result(String prefix, long time) { | |
System.out.println(prefix + ": elapsed=" + time / 1_000); | |
} | |
public static void main(String[] args) { | |
System.out.println("os.arch=" + System.getProperty("os.arch")); | |
System.out.println("os.name=" + System.getProperty("os.name")); | |
System.out.println("os.version=" + System.getProperty("os.version")); | |
System.out.println("java.version=" + System.getProperty("java.version")); | |
System.out.println("java.vm.info=" + System.getProperty("java.vm.info")); | |
System.out.println("java.vm.name=" + System.getProperty("java.vm.name")); | |
System.out.println("sun.management.compiler=" + System.getProperty("sun.management.compiler")); | |
System.out.println("poolParallelism=" + java.util.concurrent.ForkJoinPool.getCommonPoolParallelism()); | |
new Sum(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment