Last active
March 29, 2016 08:49
-
-
Save h0tk3y/bc508a573abfe8e8b3b3 to your computer and use it in GitHub Desktop.
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
| import java.util.* | |
| import kotlin.system.measureTimeMillis | |
| fun main(args: Array<String>) { | |
| val nTests = 20000 | |
| val testLength = 10000 | |
| val random = Random() | |
| val strs = (1..nTests).map { (1..testLength).map { (random.nextInt() % 10) }.joinToString() } | |
| val dropTakeTime = measureTimeMillis { strs.forEach { it.drop(1) + it.first() } } | |
| val arrayTime = measureTimeMillis { | |
| strs.forEach { str -> | |
| str.toCharArray().let { it -> | |
| for (i in 0..it.lastIndex - 1) | |
| it[i] = it[i + 1] | |
| it[it.lastIndex] = str[0] // str is unchanged | |
| String(it) | |
| } | |
| } | |
| } | |
| val builderTime = measureTimeMillis { | |
| strs.forEach { str -> | |
| var builder = StringBuilder() | |
| for (i in 0..str.length - 2) { | |
| builder.append(str[i+1]) | |
| } | |
| builder.append(str[0]) | |
| builder.toString() | |
| } | |
| } | |
| println("drop + take: $dropTakeTime ms") | |
| println("array: $arrayTime ms") | |
| println("builder: $builderTime ms") | |
| } | |
| // drop + take: 1076 ms | |
| // array: 1038 ms | |
| // builder: 4435 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment