Skip to content

Instantly share code, notes, and snippets.

@amaembo
Created October 24, 2023 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amaembo/fd82f368be71f44480c522f8766bfd89 to your computer and use it in GitHub Desktop.
Save amaembo/fd82f368be71f44480c522f8766bfd89 to your computer and use it in GitHub Desktop.
Benchmark puzzle
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