Skip to content

Instantly share code, notes, and snippets.

@cowlike
Last active March 13, 2018 15:22
Show Gist options
  • Save cowlike/a9322c26d26dfb631a8a01b715312a84 to your computer and use it in GitHub Desktop.
Save cowlike/a9322c26d26dfb631a8a01b715312a84 to your computer and use it in GitHub Desktop.
Simple testing with Kotlin
fun doTests() {
// println("run test 1")
// runTest(::test1)
println("\nrun test 2")
runTest(::test2)
println("\nrun test 3")
runTest(::test3)
}
fun runTest(test: (() -> String)) {
println("Warming up Kotlin")
test()
test()
test()
println("Measuring Kotlin")
val time = (1..10).map { measureTimeMillis { test() } }
println("Tests took ${time.sum()} ms, average run ${time.average()} ms")
}
fun trans(nucleotide: Char) =
when (nucleotide) {
'G' -> 'C'
'C' -> 'G'
'T' -> 'A'
else -> 'U'
}
val testDna = "GGCTCCGAAA".repeat(1_000_000)
fun test1() =
testDna.fold("") { rna, nucleotide ->
rna + trans(nucleotide.toUpperCase())
}
fun test2() =
testDna.fold(StringBuffer()) { rna, nucleotide ->
rna.append(trans(nucleotide.toUpperCase()))
}.toString()
fun test3() = testDna.map { trans(it.toUpperCase()) }.joinToString("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment