Created
October 19, 2020 11:16
-
-
Save crakaC/a526f66976708af5e58b8dc9ff873bd6 to your computer and use it in GitHub Desktop.
String::toIntとInteger::parseIntの差はほとんどないっぽい
This file contains 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 kotlin.system.* | |
fun main(){ | |
val N = 1_000_000 | |
val K = 100 | |
val stringNums = (1..N).map(Int::toString) | |
run{ | |
println("it.toInt()") | |
val t = average(K){ | |
stringNums.map{it.toInt()} | |
} | |
println(t) | |
} | |
run{ | |
println("String::toInt") | |
val t = average(K){ | |
stringNums.map(String::toInt) | |
} | |
println(t) | |
} | |
run{ | |
println("Integer::parseInt") | |
val t = average(K){ | |
stringNums.map(Integer::parseInt) | |
} | |
println(t) | |
} | |
} | |
fun average(n: Int, block: () -> Unit) = (1..n).map{ | |
System.gc() | |
System.runFinalization() | |
measureTimeMillis{ | |
block() | |
} | |
}.average() | |
/***** output ***** | |
it.toInt() | |
29.11 | |
String::toInt | |
28.92 | |
Integer::parseInt | |
29.83 | |
*******************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
時間計測する前にちゃんとgcするのがミソ