Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
}
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
@ilya-g

This comment has been minimized.

Copy link

@ilya-g ilya-g commented Dec 28, 2017

Could you compare the allocation rate in the same units?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment