Created
May 11, 2016 13:09
-
-
Save SleimanJneidi/1ca9ef2c917f884e547881a191c2d2e1 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
@State(Scope.Thread) | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.MILLISECONDS) | |
public class SomeBenchmark { | |
private List<String> stringList = new ArrayList<>(); | |
@Setup | |
public void setup() throws Exception{ | |
for (int i = 0; i < 10_000_0; i++) { | |
stringList.add("String" + i); | |
} | |
} | |
@Benchmark | |
public String withStreams(){ | |
Stream <String> stream = stringList.stream().filter(s -> s.contains("99")); | |
return stream.findFirst().get(); | |
} | |
@Benchmark | |
public String withLoops(){ | |
String result = null; | |
for (String ss1 : stringList) { | |
if (ss1.contains("99")) { | |
result = ss1; | |
break; | |
} | |
} | |
return result; | |
} | |
public static void main(String[] args) throws Exception { | |
Options options = new OptionsBuilder() | |
.include(SomeBenchmark.class.getSimpleName()) | |
.warmupIterations(3) | |
.measurementIterations(10) | |
.forks(1) | |
.build(); | |
Runner runner = new Runner(options); | |
runner.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment