Skip to content

Instantly share code, notes, and snippets.

@morgen-peschke
Last active August 29, 2015 14:14
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 morgen-peschke/6c733744ceb9d1821ccf to your computer and use it in GitHub Desktop.
Save morgen-peschke/6c733744ceb9d1821ccf to your computer and use it in GitHub Desktop.
Java Create List Benchmark
/* Results:
| Test Name | Time per round | Time in GC | Time in Benchmark |
|--------------------------------------------------+----------------+------------+-------------------|
| LinkedList Add only in front | 0.03 [+- 0.00] | 2.67 | 3.06 |
| LinkedList Add only in back | 0.03 [+- 0.01] | 2.61 | 3.01 |
| LinkedList Add in front and back | 0.03 [+- 0.00] | 2.66 | 3.16 |
| ArrayList Add only in front | 3.83 [+- 0.06] | 0.74 | 382.93 |
| ArrayList Add only in front with known size | 3.87 [+- 0.05] | 0.76 | 386.71 |
| ArrayList Add only in back | 0.01 [+- 0.01] | 0.69 | 0.87 |
| ArrayList Add only in back with known size | 0.01 [+- 0.01] | 0.76 | 0.93 |
| ArrayList Add in front and back | 2.00 [+- 0.08] | 0.73 | 199.96 |
| ArrayList Add in front and back with known size | 1.97 [+- 0.05] | 0.77 | 197.39 |
=== Raw ==============================
PerformanceTests.ArrayListAddBack: [measured 100 out of 105 rounds, threads: 1 (sequential)]
round: 0.01 [+- 0.01], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 150, GC.time: 0.69, time.total: 0.92, time.warmup: 0.05, time.bench: 0.87
PerformanceTests.LinkedListAddFront: [measured 100 out of 105 rounds, threads: 1 (sequential)]
round: 0.03 [+- 0.00], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 294, GC.time: 2.67, time.total: 3.23, time.warmup: 0.16, time.bench: 3.06
PerformanceTests.ArrayListAddFront: [measured 100 out of 105 rounds, threads: 1 (sequential)]
round: 3.83 [+- 0.06], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 150, GC.time: 0.74, time.total: 402.05, time.warmup: 19.12, time.bench: 382.93
PerformanceTests.LinkedListAddBack: [measured 100 out of 105 rounds, threads: 1 (sequential)]
round: 0.03 [+- 0.01], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 281, GC.time: 2.61, time.total: 3.15, time.warmup: 0.13, time.bench: 3.01
PerformanceTests.ArrayListAddFrontWithKnownSize: [measured 100 out of 105 rounds, threads: 1 (sequential)]
round: 3.87 [+- 0.05], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 143, GC.time: 0.76, time.total: 406.02, time.warmup: 19.31, time.bench: 386.71
PerformanceTests.ArrayListAddFrontAndBackWithKnownSize: [measured 100 out of 105 rounds, threads: 1 (sequential)]
round: 1.97 [+- 0.05], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 143, GC.time: 0.77, time.total: 207.46, time.warmup: 10.08, time.bench: 197.39
PerformanceTests.ArrayListAddBackWithKnownSize: [measured 100 out of 105 rounds, threads: 1 (sequential)]
round: 0.01 [+- 0.01], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 143, GC.time: 0.76, time.total: 0.98, time.warmup: 0.05, time.bench: 0.93
PerformanceTests.LinkedListAddFrontAndBack: [measured 100 out of 105 rounds, threads: 1 (sequential)]
round: 0.03 [+- 0.00], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 292, GC.time: 2.66, time.total: 3.34, time.warmup: 0.19, time.bench: 3.16
PerformanceTests.ArrayListAddFrontAndBack: [measured 100 out of 105 rounds, threads: 1 (sequential)]
round: 2.00 [+- 0.08], round.block: 0.00 [+- 0.00], round.gc: 0.00 [+- 0.00], GC.calls: 150, GC.time: 0.73, time.total: 209.64, time.warmup: 9.68, time.bench: 199.96
Tests run: 9, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1,236.847 sec
*/
import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
import com.carrotsearch.junitbenchmarks.BenchmarkRule;
import java.util.ArrayList;
import java.util.LinkedList;
import static org.junit.Assert.assertFalse;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
@BenchmarkOptions(benchmarkRounds=100)
public class PerformanceTests {
@Rule
public TestRule benchmarkRun = new BenchmarkRule();
private static final int TRIALS = 100_000;
// <editor-fold desc="Add front" defaultstate="collapsed">
@Test
public void LinkedListAddFront() {
LinkedList<Integer> list = new LinkedList<>();
for (int i = 0; i < TRIALS; i += 1) {
list.addFirst(i);
list.addFirst(i);
}
ArrayList<Integer> return_list = new ArrayList<>(list);
assertFalse(return_list.isEmpty());
}
@Test
public void ArrayListAddFront() {
ArrayList<Integer> list = new ArrayList<>(0);
for (int i = 0; i < TRIALS; i += 1) {
list.add(0, i);
list.add(0, i);
}
assertFalse(list.isEmpty());
}
@Test
public void ArrayListAddFrontWithKnownSize() {
ArrayList<Integer> list = new ArrayList<>(TRIALS);
for (int i = 0; i < TRIALS; i += 1) {
list.add(0, i);
list.add(0, i);
}
assertFalse(list.isEmpty());
}
// </editor-fold>
// <editor-fold desc="Add front and back" defaultstate="collapsed">
@Test
public void LinkedListAddBack() {
LinkedList<Integer> list = new LinkedList<>();
for (int i = 0; i < TRIALS; i += 1) {
list.addLast(i);
list.addLast(i);
}
ArrayList<Integer> return_list = new ArrayList<>(list);
assertFalse(return_list.isEmpty());
}
@Test
public void ArrayListAddBack() {
ArrayList<Integer> list = new ArrayList<>(0);
for (int i = 0; i < TRIALS; i += 1) {
list.add(i);
list.add(i);
}
assertFalse(list.isEmpty());
}
@Test
public void ArrayListAddBackWithKnownSize() {
ArrayList<Integer> list = new ArrayList<>(TRIALS);
for (int i = 0; i < TRIALS; i += 1) {
list.add(i);
list.add(i);
}
assertFalse(list.isEmpty());
}
// </editor-fold>
// <editor-fold desc="Add front and back" defaultstate="collapsed">
@Test
public void LinkedListAddFrontAndBack() {
LinkedList<Integer> list = new LinkedList<>();
for (int i = 0; i < TRIALS; i += 1) {
list.addFirst(i);
list.addLast(i);
}
ArrayList<Integer> return_list = new ArrayList<>(list);
assertFalse(return_list.isEmpty());
}
@Test
public void ArrayListAddFrontAndBack() {
ArrayList<Integer> list = new ArrayList<>(0);
for (int i = 0; i < TRIALS; i += 1) {
list.add(0, i);
list.add(i);
}
assertFalse(list.isEmpty());
}
@Test
public void ArrayListAddFrontAndBackWithKnownSize() {
ArrayList<Integer> list = new ArrayList<>(TRIALS);
for (int i = 0; i < TRIALS; i += 1) {
list.add(0, i);
list.add(i);
}
assertFalse(list.isEmpty());
}
// </editor-fold>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment