Created
December 28, 2017 14:54
-
-
Save qwwdfsad/a130bb925b75e0354277bc23d6a5f2fa 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.qwwdfsad.benchmarks | |
import org.openjdk.jmh.annotations.* | |
import java.util.concurrent.ThreadLocalRandom | |
import java.util.concurrent.TimeUnit | |
@Fork(value = 2) //jvmArgsAppend = arrayOf("-XX:TieredStopAtLevel=1")) | |
@Measurement(iterations = 10) | |
@Warmup(iterations = 10) | |
@State(Scope.Benchmark) | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
open class IndexOfBenchmark { | |
private val haystack = "haystack" | |
private val needles: CharArray = "_!".toCharArray() | |
@Benchmark | |
fun builtInAnyIndexOf(): Int { | |
return haystack.findAnyOf(needles, 0, false, ThreadLocalRandom.current().nextBoolean())?.first ?: -1 | |
} | |
@Benchmark | |
fun fastAnyIndexOf(): Int { | |
return haystack.fastFindAnyOf(needles, 0, false, ThreadLocalRandom.current().nextBoolean())?.first ?: -1 | |
} | |
// Copy of std-private implementation | |
private fun CharSequence.findAnyOf(chars: CharArray, startIndex: Int, ignoreCase: Boolean, last: Boolean): Pair<Int, Char>? { | |
if (!ignoreCase && chars.size == 1 && this is String) { | |
throw RuntimeException() | |
} | |
val indices = if (!last) Math.max(startIndex, 0)..lastIndex else Math.min(startIndex, lastIndex) downTo 0 | |
for (index in indices) { | |
val charAtIndex = get(index) | |
val matchingCharIndex = chars.indexOfFirst { it.equals(charAtIndex, ignoreCase) } | |
if (matchingCharIndex >= 0) | |
return index to chars[matchingCharIndex] | |
} | |
return null | |
} | |
private fun CharSequence.fastFindAnyOf(chars: CharArray, startIndex: Int, ignoreCase: Boolean, last: Boolean): Pair<Int, Char>? { | |
if (!ignoreCase && chars.size == 1 && this is String) { | |
throw RuntimeException() | |
} | |
return if (!last) { | |
fastFindFirstOf(chars, startIndex, false) | |
} else { | |
fastFindLastOf(chars, startIndex, false) | |
} | |
} | |
private fun CharSequence.fastFindFirstOf(chars: CharArray, startIndex: Int, ignoreCase: Boolean): Pair<Int, Char>? { | |
for (index in Math.max(startIndex, 0)..lastIndex) { | |
val charAtIndex = get(index) | |
val matchingCharIndex = chars.indexOfFirst { it.equals(charAtIndex, ignoreCase) } | |
if (matchingCharIndex >= 0) | |
return index to chars[matchingCharIndex] | |
} | |
return null | |
} | |
private fun CharSequence.fastFindLastOf(chars: CharArray, startIndex: Int, ignoreCase: Boolean): Pair<Int, Char>? { | |
for (index in Math.min(startIndex, lastIndex) downTo 0) { | |
val charAtIndex = get(index) | |
val matchingCharIndex = chars.indexOfFirst { it.equals(charAtIndex, ignoreCase) } | |
if (matchingCharIndex >= 0) | |
return index to chars[matchingCharIndex] | |
} | |
return null | |
} | |
} |
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
Benchmark Mode Cnt Score Error Units | |
IndexOfBenchmark.builtInAnyIndexOf avgt 20 26.241 ± 2.061 ns/op | |
IndexOfBenchmark.fastAnyIndexOf avgt 20 22.951 ± 1.071 ns/op | |
IndexOfBenchmark.builtInAnyIndexOf:·gc.alloc.rate.norm avgt 20 24.000 ± 0.001 B/op | |
IndexOfBenchmark.fastAnyIndexOf:·gc.alloc.rate avgt 20 0.002 ± 0.003 MB/sec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you compare the allocation rate in the same units?