Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kotlin issue with iterating through ranges
fun test1() {
var x = 1f
var y = 0
for (a in 1 .. 1000) {
for (b in 1 .. 1000) {
for (c in 1 .. 1000) {
x *= -1f
y += 1
}
}
}
}
fun test2() {
var x = 1f
var y = 0
for (a in 1 until 1001) {
for (b in 1 until 1001) {
for (c in 1 until 1001) {
x *= -1f
y += 1
}
}
}
}
fun main() {
var time = 0L
time = System.nanoTime()
test1()
println("Result using '..': ${(System.nanoTime() - time) / 1000000f} ms.")
time = System.nanoTime()
test2()
println("Result using 'until': ${(System.nanoTime() - time) / 1000000f} ms.")
println()
println()
time = System.nanoTime()
test2()
println("Result using 'until': ${(System.nanoTime() - time) / 1000000f} ms.")
time = System.nanoTime()
test1()
println("Result using '..': ${(System.nanoTime() - time) / 1000000f} ms.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.