Skip to content

Instantly share code, notes, and snippets.

@SleimanJneidi
Created May 11, 2016 13:09
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 SleimanJneidi/1ca9ef2c917f884e547881a191c2d2e1 to your computer and use it in GitHub Desktop.
Save SleimanJneidi/1ca9ef2c917f884e547881a191c2d2e1 to your computer and use it in GitHub Desktop.
@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