Last active
November 11, 2016 12:23
-
-
Save lukaseder/7e29ef47c50eef35e66de18d2fb9dcfd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jooq.test.benchmark; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Level; | |
import org.openjdk.jmh.annotations.Measurement; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.TearDown; | |
import org.openjdk.jmh.annotations.Warmup; | |
import org.openjdk.jmh.infra.Blackhole; | |
@Fork(value = 3, jvmArgsAppend = "-Djmh.stack.lines=3") | |
@Warmup(iterations = 5) | |
@Measurement(iterations = 7) | |
public class NullCheck { | |
@State(Scope.Benchmark) | |
public static class BenchmarkState { | |
List<Integer> list = new ArrayList<Integer>(); | |
@Setup(Level.Trial) | |
public void setup() throws Exception { | |
for (int i = 0; i < 100; i++) | |
list.add(i); | |
} | |
@TearDown(Level.Trial) | |
public void teardown() throws Exception { | |
list = null; | |
} | |
} | |
@Benchmark | |
public void testNullAndEmptyCheck(Blackhole blackhole, BenchmarkState state) { | |
if (state.list == null || state.list.isEmpty()) | |
return; | |
for (Integer i : state.list) | |
blackhole.consume(i); | |
} | |
@Benchmark | |
public void testNoEmptyCheck(Blackhole blackhole, BenchmarkState state) { | |
if (state.list == null) | |
return; | |
for (Integer i : state.list) | |
blackhole.consume(i); | |
} | |
@Benchmark | |
public void testNoCheck(Blackhole blackhole, BenchmarkState state) { | |
for (Integer i : state.list) | |
blackhole.consume(i); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment