Skip to content

Instantly share code, notes, and snippets.

@jeffque
Last active November 23, 2025 18:47
Show Gist options
  • Select an option

  • Save jeffque/423c4655b4ea82c4a2c8ced51595c2a6 to your computer and use it in GitHub Desktop.

Select an option

Save jeffque/423c4655b4ea82c4a2c8ced51595c2a6 to your computer and use it in GitHub Desktop.
Benchmark de soma de iteração de arraylist vs stream
package jeffque.bench;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
@Warmup(iterations = 2)
@Measurement(iterations = 3, time = 2, timeUnit = TimeUnit.SECONDS)
@Timeout(time = 500, timeUnit = TimeUnit.SECONDS)
@BenchmarkMode(Mode.Throughput)
public class Summation {
@State(Scope.Benchmark)
public static class Input {
// @Param({ "10", "100", "1000", "100000", "10000000", "50000000" })
@Param({ "100000", "50000000" })
public int size;
List<Integer> lista;
@Setup(Level.Trial)
public void criaEstado() {
final var r = new Random(15);
lista = IntStream.range(0, size)
.mapToObj(id -> r.nextInt())
.toList();
}
}
@Benchmark
public long withForIndex(Input i) {
long result = 0;
for (int idx = 0; idx < i.lista.size(); idx++) {
result += i.lista.get(idx);
}
return result;
}
@Benchmark
public long withForeach(Input i) {
long result = 0;
for (int l: i.lista) {
result += l;
}
return result;
}
@Benchmark
public long withStream(Input i) {
return i.lista.stream().mapToInt(Integer::intValue).sum();
}
@Benchmark
public void noopForeach(Input i, Blackhole b) {
for (int l: i.lista) {
b.consume(l);
}
}
@Benchmark
public int withForeachInt(Input i) {
int result = 0;
for (int l: i.lista) {
result += l;
}
return result;
}
// baseado em https://www.baeldung.com/java-streams-vs-loops
@Benchmark
public int withStreamAsWrapper(Input i) {
return i.lista.stream().reduce(0, Integer::sum);
}
}
@jeffque
Copy link
Author

jeffque commented Aug 22, 2025

Resultados que eu obtive localmente:

Benchmark                 (size)   Mode  Cnt      Score     Error  Units
Summation.noopForeach     100000  thrpt   15  24917.405 ±  75.933  ops/s
Summation.noopForeach   50000000  thrpt   15     38.930 ±   0.397  ops/s
Summation.withForIndex    100000  thrpt   15  15457.297 ± 414.089  ops/s
Summation.withForIndex  50000000  thrpt   15     28.070 ±   0.421  ops/s
Summation.withForeach     100000  thrpt   15  15472.856 ± 196.674  ops/s
Summation.withForeach   50000000  thrpt   15     27.336 ±   1.675  ops/s
Summation.withStream      100000  thrpt   15  19552.178 ± 566.742  ops/s
Summation.withStream    50000000  thrpt   15     35.858 ±   0.419  ops/s

@jeffque
Copy link
Author

jeffque commented Nov 23, 2025

Resultados que obtive localmente após adicionar a função withStreamAsWrapper (para simular o resultado de https://www.baeldung.com/java-streams-vs-loops) e usando int em withForeachInt como tipo acumulador:

Benchmark                        (size)   Mode  Cnt      Score     Error  Units
Summation.noopForeach            100000  thrpt   15  25074.165 ± 145.263  ops/s
Summation.noopForeach          50000000  thrpt   15     39.605 ±   0.493  ops/s
Summation.withForIndex           100000  thrpt   15  15779.702 ±  23.341  ops/s
Summation.withForIndex         50000000  thrpt   15     28.676 ±   0.109  ops/s
Summation.withForeach            100000  thrpt   15  15765.148 ±  21.495  ops/s
Summation.withForeach          50000000  thrpt   15     28.494 ±   0.305  ops/s
Summation.withForeachInt         100000  thrpt   15  24297.451 ± 125.790  ops/s
Summation.withForeachInt       50000000  thrpt   15     38.663 ±   0.238  ops/s
Summation.withStream             100000  thrpt   15  19967.495 ± 113.473  ops/s
Summation.withStream           50000000  thrpt   15     36.249 ±   0.916  ops/s
Summation.withStreamAsWrapper    100000  thrpt   15   3804.950 ±  63.529  ops/s
Summation.withStreamAsWrapper  50000000  thrpt   15      5.544 ±   1.966  ops/s

Dados sobre ambiente de test:

JMH version: 1.37
VM version: JDK 21.0.3, OpenJDK 64-Bit Server VM, 21.0.3+9-LTS

Rodado em um MacBook Pro, macOs 15.6.1, 15 GB de RAM, processador Apple M1 Pro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment