Skip to content

Instantly share code, notes, and snippets.

@Mishkun
Last active October 10, 2018 08:52
Show Gist options
  • Save Mishkun/d5ef92c108ab086a1ed3253e6b16124c to your computer and use it in GitHub Desktop.
Save Mishkun/d5ef92c108ab086a1ed3253e6b16124c to your computer and use it in GitHub Desktop.
package jmh
import org.openjdk.jmh.annotations.*
import org.openjdk.jmh.infra.Blackhole
@State(Scope.Benchmark)
@Fork(1)
open class KotlinLazy {
@State(Scope.Thread)
open class LazyHashcodeState {
val value = LazyHash(
"ddbba5d9-1477-4c2e-81d4-1a33d5377f05",
"f69105f7-96d8-49d6-8b0e-6d3e2e096ee0",
"f356f4ed-efb4-4176-aeb7-9dcad1d80e2a",
149544948149544948
)
}
@State(Scope.Thread)
open class EagerHashcodeState {
val value = DataHash(
"68882523-a257-4177-a32b-bb1c23404eed",
"43a3f0b0-af33-4aa4-a8d6-f5f621e46749",
"24785ba0-5efc-4684-a2e7-9592bec2fa12",
122046460122046460
)
}
@Benchmark
fun lazyHash(bh: Blackhole, state: LazyHashcodeState): Int {
return state.value.hashCode()
}
@Benchmark
fun eagerHash(bh: Blackhole, state: EagerHashcodeState): Int {
return state.value.hashCode()
}
}
data class DataHash(val uuid1: String, val uuid2: String, var uuid3:String, val id: Long)
class LazyHash(val uuid1: String, val uuid2: String, var uuid3:String, val id: Long) {
private var memoizedHashCode = 0
private var memoized = false
override fun hashCode(): Int {
return if(memoized) {
31 * memoizedHashCode + uuid3.hashCode()
} else {
var result = uuid1.hashCode()
result = 31 * result + uuid2.hashCode()
result = 31 * result + id.hashCode()
31 * result.also { memoized = true; memoizedHashCode = it } + uuid3.hashCode()
}
}
}
#### Synopsis:
Benchmark Mode Cnt Score Error Units
KotlinLazy.eagerHash thrpt 10 166312414.794 ± 4644407.477 ops/s
KotlinLazy.lazyHash thrpt 10 250935666.677 ± 2535249.580 ops/s
KotlinLazy.eagerHash avgt 10 5.922 ± 0.032 ns/op
KotlinLazy.lazyHash avgt 10 3.991 ± 0.012 ns/op
#### Detailed:
Result "jmh.KotlinLazy.eagerHash":
166312414.794 ?(99.9%) 4644407.477 ops/s [Average]
(min, avg, max) = (159543759.913, 166312414.794, 169538194.601), stdev = 3071987.982
CI (99.9%): [161668007.318, 170956822.271] (assumes normal distribution)
Result "jmh.KotlinLazy.lazyHash":
250935666.677 ?(99.9%) 2535249.580 ops/s [Average]
(min, avg, max) = (248093843.695, 250935666.677, 253547801.570), stdev = 1676910.624
CI (99.9%): [248400417.097, 253470916.257] (assumes normal distribution)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment