Last active
July 22, 2016 15:09
-
-
Save fboldog/a5963c06afef90952ac5f2a6807bc01d to your computer and use it in GitHub Desktop.
Shakespeare plays scrabble with Kotlin for https://github.com/akarnokd/akarnokd-misc/tree/master/src/jmh/java/hu/akarnokd/comparison
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
diff --git a/build.gradle b/build.gradle | |
index afe783b..02ed7d1 100644 | |
--- a/build.gradle | |
+++ b/build.gradle | |
@@ -1,4 +1,6 @@ | |
buildscript { | |
+ ext.kotlin_version = '1.0.3' | |
+ | |
repositories { | |
mavenCentral() | |
maven { | |
@@ -8,10 +10,13 @@ buildscript { | |
dependencies { | |
classpath 'nl.javadude.gradle.plugins:license-gradle-plugin:0.9.0' | |
- classpath "me.champeau.gradle:jmh-gradle-plugin:0.2.0" | |
+ classpath 'me.champeau.gradle:jmh-gradle-plugin:0.3.1' | |
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | |
} | |
} | |
+apply plugin: 'kotlin' | |
+ | |
apply plugin: 'java' | |
apply plugin: 'eclipse' | |
@@ -108,6 +113,8 @@ dependencies { | |
//compile 'io.swave:swave-core_2.11:0.5-M1' | |
compile 'it.unimi.dsi:fastutil:7.0.12' | |
+ | |
+ compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | |
} | |
processResources { | |
@@ -147,12 +154,10 @@ apply plugin: 'license' | |
apply from: file('gradle/license.gradle') | |
jmh { | |
- jmhVersion = '1.11.3' | |
+ jmhVersion = '1.12' | |
humanOutputFile = null | |
if (project.hasProperty('jmh')) { | |
- include = ".*" + project.jmh + ".*" | |
- } else { | |
- include = ".*" | |
+ include = ".*ShakespearePlaysScrabbleWith.*" | |
} | |
} | |
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
Benchmark (cores) (fj) (mode) (prefetch) Mode Cnt Score Error Units | |
ShakespearePlaysScrabbleWithGuava.measureThroughput N/A N/A N/A N/A sample 59 89.020 ± 3.061 ms/op | |
ShakespearePlaysScrabbleWithGuavaOpt.measureThroughput N/A N/A N/A N/A sample 126 40.859 ± 1.244 ms/op | |
ShakespearePlaysScrabbleWithIx.measureThroughput N/A N/A N/A N/A sample 70 76.467 ± 0.521 ms/op | |
ShakespearePlaysScrabbleWithIxOpt.measureThroughput N/A N/A N/A N/A sample 197 25.672 ± 0.639 ms/op | |
ShakespearePlaysScrabbleWithKotlin.measureThroughput N/A N/A N/A N/A sample 222 22.858 ± 0.358 ms/op | |
ShakespearePlaysScrabbleWithNonParallelStreams.measureThroughput N/A N/A N/A N/A sample 184 27.581 ± 0.412 ms/op | |
ShakespearePlaysScrabbleWithParallelStreams.measureThroughput N/A N/A N/A N/A sample 561 8.975 ± 0.125 ms/op | |
ShakespearePlaysScrabbleWithReactor25.measureThroughput N/A N/A N/A N/A sample 40 139.657 ± 1.836 ms/op | |
ShakespearePlaysScrabbleWithReactor25Opt.measureThroughput N/A N/A N/A N/A sample 146 35.172 ± 1.187 ms/op | |
ShakespearePlaysScrabbleWithReactor25ParallelOpt.measureThroughput 8 N/A parallel N/A sample 510 9.927 ± 0.443 ms/op | |
ShakespearePlaysScrabbleWithRsc.measureThroughput N/A N/A N/A N/A sample 39 132.366 ± 8.887 ms/op | |
ShakespearePlaysScrabbleWithRscOpt.measureThroughput N/A N/A N/A N/A sample 150 34.268 ± 1.186 ms/op | |
ShakespearePlaysScrabbleWithRscParallelOpt.measureThroughput 8 false N/A 128 sample 577 8.704 ± 0.112 ms/op | |
ShakespearePlaysScrabbleWithRxJava1.measureThroughput N/A N/A N/A N/A sample 35 158.223 ± 2.816 ms/op | |
ShakespearePlaysScrabbleWithRxJava1Opt.measureThroughput N/A N/A N/A N/A sample 70 75.629 ± 1.381 ms/op | |
ShakespearePlaysScrabbleWithRxJava2Flowable.measureThroughput N/A N/A N/A N/A sample 41 126.341 ± 2.643 ms/op | |
ShakespearePlaysScrabbleWithRxJava2FlowableOpt.measureThroughput N/A N/A N/A N/A sample 130 39.375 ± 1.466 ms/op | |
ShakespearePlaysScrabbleWithRxJava2Observable.measureThroughput N/A N/A N/A N/A sample 64 81.879 ± 1.371 ms/op | |
ShakespearePlaysScrabbleWithRxJava2ObservableOpt.measureThroughput N/A N/A N/A N/A sample 163 31.324 ± 0.499 ms/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
package hu.akarnokd.comparison | |
import org.openjdk.jmh.annotations.* | |
import java.util.* | |
import java.util.concurrent.TimeUnit | |
fun main(args: Array<String>) { | |
val s = ShakespearePlaysScrabbleWithKotlin() | |
s.init() | |
System.out.println(s.measureThroughput()) | |
} | |
@State(Scope.Benchmark) | |
open class ShakespearePlaysScrabbleWithKotlin: ShakespearePlaysScrabble() { | |
@Benchmark | |
@BenchmarkMode(Mode.SampleTime) | |
@OutputTimeUnit(TimeUnit.MILLISECONDS) | |
@Warmup( | |
iterations=5 | |
) | |
@Measurement( | |
iterations=5 | |
) | |
@Fork(1) | |
fun measureThroughput(): List<Map.Entry<Int, List<String>>> { | |
val scoreOfALetter: (Char) -> Int = { letter -> | |
letterScores[letter.toInt() - letter_a] | |
} | |
val letterScore: (Map.Entry<Int, Long>) -> Int = { entry -> | |
letterScores[entry.key - letter_a] * | |
Math.min(entry.value.toInt(), scrabbleAvailableLetters[entry.key - letter_a]) | |
} | |
fun histoOfLetters(word: String): Map<Int, Long> = word.groupBy { it.toInt() }.mapValues { it.value.size.toLong() } | |
fun blank(entry: Map.Entry<Int, Long>): Long = | |
Math.max(0L, entry.value - scrabbleAvailableLetters[entry.key - letter_a]) | |
fun nBlanks(word: String): Long = histoOfLetters(word).entries.map { blank(it) }.sum() | |
fun checkBlanks(word: String) = nBlanks(word) <= 2 | |
fun score2(word: String): Int = histoOfLetters(word).entries.map(letterScore).sum() | |
fun first3(word: String): List<Char> = word.toCharArray().take(3) | |
fun last3(word: String): List<Char> = word.toCharArray().takeLast(3) | |
fun toBeMaxed(word: String): Sequence<Char> = sequenceOf(first3(word), last3(word)).flatten() | |
fun bonusForDoubleLetter(word: String): Int = toBeMaxed(word).map { scoreOfALetter(it) }.max()?:0 | |
fun score3(word: String): Int = 2 * (score2(word) + bonusForDoubleLetter(word)) + if (word.length == 7) 50 else 0 | |
fun buildHistoOnScore(score: (String) -> Int): Map<Int, List<String>> = | |
shakespeareWords | |
.filter { scrabbleWords.contains(it) } | |
.filter { checkBlanks(it) } | |
.groupByTo(TreeMap<Int, MutableList<String>>(Collections.reverseOrder()), score) | |
return buildHistoOnScore(::score3).entries.take(3) | |
} | |
val letter_a = 'a'.toInt() | |
val letterScores = arrayOf( | |
// a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z | |
1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10) | |
val scrabbleAvailableLetters = arrayOf( | |
// a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z | |
9, 2, 2, 1, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment