Benchmark Example - IntStream vs. For-Loop
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Karl Heinz Marbaise 6ff1456fbd
Some checks failed
Main / Linux (push) Failing after 10s
First Commit.
2026-11-02 14:06:26 +01:00
.github/workflows First Commit. 2026-11-02 14:06:26 +01:00
profile-results First Commit. 2026-11-02 14:06:26 +01:00
src/main/java/com/soebes/benchmark/intstream First Commit. 2026-11-02 14:06:26 +01:00
.gitignore First Commit. 2026-11-02 14:06:26 +01:00
CODE_OF_CONDUCT.md First Commit. 2026-11-02 14:06:26 +01:00
CODE_OF_CONDUCT_de.md First Commit. 2026-11-02 14:06:26 +01:00
LICENSE.txt First Commit. 2026-11-02 14:06:26 +01:00
pom.xml First Commit. 2026-11-02 14:06:26 +01:00
README.adoc First Commit. 2026-11-02 14:06:26 +01:00

Comparing

var result = IntStream.range(0,1000).boxed().toList();

vs.

List<Integer> scores = new ArrayList<>();
for (int i = 0; i < 1_000_000; i++) {
    scores.add(i);
}

State

  • Working on it.

Requirements

  • Maven 3.9+

  • JDK17+ (build and runtime.)

Running

mvn verify
java -jar target/intstream-1.0-SNAPSHOT.jar -prof gc:alloc=true;churn=true -prof jfr:dir=profile-results

Results

Raw Performance

Benchmark                                    (range)  Mode  Cnt       Score      Error  Units
BenchmarkIntStreamVsForLoop.benchForLoop     1000000  avgt   25    7326,380 ±  190,772  us/op
BenchmarkIntStreamVsForLoop.benchForLoop    10000000  avgt   25  131275,575 ± 1781,566  us/op
BenchmarkIntStreamVsForLoop.benchIntStream   1000000  avgt   25    2932,812 ±   22,721  us/op
BenchmarkIntStreamVsForLoop.benchIntStream  10000000  avgt   25   72963,084 ±  725,221  us/op

Memory allocation/ GC Rates

Benchmark                                                      (range)  Mode  Cnt     Score    Error   Units
BenchmarkIntStreamVsForLoop.benchForLoop                           100  avgt   25     0,260 ±  0,002   us/op
BenchmarkIntStreamVsForLoop.benchForLoop:gc.alloc.rate             100  avgt   25  5128,768 ± 35,258  MB/sec
BenchmarkIntStreamVsForLoop.benchForLoop:gc.alloc.rate.norm        100  avgt   25  1400,000 ±  0,001    B/op
BenchmarkIntStreamVsForLoop.benchForLoop:gc.count                  100  avgt   25  1982,000           counts
BenchmarkIntStreamVsForLoop.benchForLoop:gc.time                   100  avgt   25  1047,000               ms
BenchmarkIntStreamVsForLoop.benchIntStream                         100  avgt   25     0,297 ±  0,002   us/op
BenchmarkIntStreamVsForLoop.benchIntStream:gc.alloc.rate           100  avgt   25  2027,990 ± 15,844  MB/sec
BenchmarkIntStreamVsForLoop.benchIntStream:gc.alloc.rate.norm      100  avgt   25   632,000 ±  0,001    B/op
BenchmarkIntStreamVsForLoop.benchIntStream:gc.count                100  avgt   25   846,000           counts
BenchmarkIntStreamVsForLoop.benchIntStream:gc.time                 100  avgt   25   462,000               ms