Skip to content

Commit ac616c0

Browse files
committed
Add code for module 1, part 2, video 7
1 parent 5c4108e commit ac616c0

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package eu.happycoders.datastructures.module2.p2_lists.v07_empty_lists;
2+
3+
import org.openjdk.jmh.annotations.BenchmarkMode;
4+
import org.openjdk.jmh.annotations.Fork;
5+
import org.openjdk.jmh.annotations.Measurement;
6+
import org.openjdk.jmh.annotations.Mode;
7+
import org.openjdk.jmh.annotations.OutputTimeUnit;
8+
import org.openjdk.jmh.annotations.Scope;
9+
import org.openjdk.jmh.annotations.State;
10+
import org.openjdk.jmh.annotations.Warmup;
11+
12+
import java.util.concurrent.TimeUnit;
13+
14+
@State(Scope.Thread)
15+
@BenchmarkMode(Mode.AverageTime)
16+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
17+
18+
@Warmup(iterations = 2, time = 5)
19+
@Measurement(iterations = 5, time = 5)
20+
@Fork(1)
21+
public class EmptyListBenchmark {
22+
23+
24+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package eu.happycoders.datastructures.module2.p2_lists.v07_empty_lists;
2+
3+
import org.openjdk.jmh.annotations.Benchmark;
4+
import org.openjdk.jmh.annotations.BenchmarkMode;
5+
import org.openjdk.jmh.annotations.Fork;
6+
import org.openjdk.jmh.annotations.Level;
7+
import org.openjdk.jmh.annotations.Measurement;
8+
import org.openjdk.jmh.annotations.Mode;
9+
import org.openjdk.jmh.annotations.OutputTimeUnit;
10+
import org.openjdk.jmh.annotations.Param;
11+
import org.openjdk.jmh.annotations.Scope;
12+
import org.openjdk.jmh.annotations.Setup;
13+
import org.openjdk.jmh.annotations.State;
14+
import org.openjdk.jmh.annotations.Warmup;
15+
import org.openjdk.jmh.infra.Blackhole;
16+
17+
import java.util.ArrayList;
18+
import java.util.Collections;
19+
import java.util.List;
20+
import java.util.concurrent.TimeUnit;
21+
22+
@State(Scope.Thread)
23+
@BenchmarkMode(Mode.AverageTime)
24+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
25+
26+
@Warmup(iterations = 2, time = 5)
27+
@Measurement(iterations = 5, time = 5)
28+
@Fork(1)
29+
public class EmptyListBenchmarkSolution {
30+
31+
private List<String> list;
32+
33+
@Param({"ArrayList", "List.of()", "emptyList()"})
34+
private String listType;
35+
36+
@Setup(Level.Trial)
37+
public void setup() {
38+
list = switch (listType) {
39+
case "ArrayList" -> new ArrayList<>();
40+
case "List.of()" -> List.of();
41+
case "emptyList()" -> Collections.emptyList();
42+
default -> throw new IllegalStateException();
43+
};
44+
}
45+
46+
@Benchmark
47+
public int size() {
48+
return list.size();
49+
}
50+
51+
@Benchmark
52+
public boolean isEmpty() {
53+
return list.isEmpty();
54+
}
55+
56+
@Benchmark
57+
public int indexOf() {
58+
return list.indexOf("element");
59+
}
60+
61+
@Benchmark
62+
public boolean contains() {
63+
return list.contains("element");
64+
}
65+
66+
@Benchmark
67+
public void iteration(Blackhole blackhole) {
68+
for (String s : list) {
69+
blackhole.consume(s);
70+
}
71+
}
72+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
void main() {
2+
3+
4+
}

0 commit comments

Comments
 (0)