Created
September 20, 2015 03:27
-
-
Save amaembo/1b59d40cf5a87d79d372 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# JMH 1.10.3 (released 65 days ago) | |
# VM version: JDK 1.8.0_45, VM 25.45-b02 | |
# VM invoker: C:\Program Files\Java\jre1.8.0_45\bin\java.exe | |
... | |
Benchmark (len) Mode Cnt Score Error Units | |
SubstringTest.LukasEder 10 avgt 30 1.947 ± 0.012 us/op | |
SubstringTest.LukasEder 100 avgt 30 151.660 ± 0.524 us/op | |
SubstringTest.LukasEder 1000 avgt 30 52405.761 ± 183.921 us/op | |
SubstringTest.TagirValeev 10 avgt 30 1.712 ± 0.018 us/op | |
SubstringTest.TagirValeev 100 avgt 30 138.179 ± 5.063 us/op | |
SubstringTest.TagirValeev 1000 avgt 30 48188.499 ± 107.321 us/op |
This file contains hidden or 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
import java.util.concurrent.TimeUnit; | |
import static java.util.stream.Collectors.*; | |
import java.util.stream.*; | |
import java.util.function.*; | |
import java.util.*; | |
import org.openjdk.jmh.infra.Blackhole; | |
import org.openjdk.jmh.annotations.*; | |
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS) | |
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.MICROSECONDS) | |
@Fork(3) | |
@State(Scope.Benchmark) | |
public class SubstringTest { | |
@Param({"10", "100", "1000"}) | |
private int len; | |
private String string; | |
@Setup | |
public void setup() { | |
string = new Random(1).ints(len, 'a', 'z'+1) | |
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString(); | |
} | |
@Benchmark | |
public int LukasEder() { | |
return IntStream.range(0, string.length()) | |
.boxed() | |
.flatMap(start -> IntStream | |
.rangeClosed(start + 1, string.length()) | |
.mapToObj(stop -> new AbstractMap.SimpleEntry<>(start, stop)) | |
) | |
.map(e -> string.substring(e.getKey(), e.getValue())) | |
.collect(summingInt(String::length)); | |
} | |
@Benchmark | |
public int TagirValeev() { | |
return IntStream.range(0, string.length()) | |
.mapToObj(start -> IntStream.rangeClosed(start+1, string.length()) | |
.mapToObj(end -> string.substring(start, end))) | |
.flatMap(Function.identity()) | |
.collect(summingInt(String::length)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment