Created
October 24, 2023 10:09
-
-
Save amaembo/fd82f368be71f44480c522f8766bfd89 to your computer and use it in GitHub Desktop.
Benchmark puzzle
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.IntSupplier; | |
import java.util.stream.IntStream; | |
public class Benchmark { | |
private int compute() { | |
IntSupplier s1 = () -> IntStream.range(0, 10000).map(v -> 1).sum(); | |
IntSupplier s2 = () -> IntStream.range(0, 10000).map(v -> 1).sum(); | |
IntSupplier s3 = () -> IntStream.range(0, 10000).map(v -> 1).sum(); | |
return s1.getAsInt() + s2.getAsInt() + s3.getAsInt(); | |
} | |
private void measure() { | |
int res = 0; | |
// Warmup | |
for (int i = 0; i < 20000; i++) { | |
res += compute(); | |
} | |
// Measurement | |
long start = System.currentTimeMillis(); | |
for (int i = 0; i < 20000; i++) { | |
res += compute(); | |
} | |
long end = System.currentTimeMillis(); | |
System.out.println(res); | |
System.out.println("Duration: " + (end - start) + "ms"); | |
} | |
public static void main(String[] args) { | |
new Benchmark().measure(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment