Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ mvn archetype:generate
-Dversion=1.0-SNAPSHOT
```

### Workshop Exercise

`io.crowdcode.java.benchmarks.exercise.ContainsBenchmark` is an unfinished benchmark with `TODO` markers.
It does not show up in `java -jar target/benchmarks.jar -l` until the first benchmark method is added.
The task description and the solution are part of the CROWDCODE workshop material.

## Benchmark Results

REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.crowdcode.java.benchmarks.exercise;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Exercise: from which collection size on does a {@link HashSet} pay off
* compared to an {@link ArrayList} when calling {@code contains()}?
*/
// TODO 1: turn this class into a JMH state object with one instance per benchmark thread
public class ContainsBenchmark {

// TODO 2: let JMH run the benchmark for the sizes 1, 10, 1000 and 100000
int size;

List<Integer> list;
Set<Integer> set;
Integer searched;

// TODO 3: let JMH call this method once before each trial
public void fillCollections() {
list = new ArrayList<>(size);
set = new HashSet<>(size);
for (int i = 0; i < size; i++) {
list.add(i);
set.add(i);
}
// worst case for the list: the searched element is the last one
searched = size - 1;
}

// TODO 4: add a benchmark method "listContains" that searches the element in the list

// TODO 5: add a benchmark method "setContains" that searches the element in the set

}
Loading