Skip to content

Instantly share code, notes, and snippets.

@alexanderpp
Created July 9, 2019 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderpp/9b63b0a242b4fc5f127aadd15278f4e3 to your computer and use it in GitHub Desktop.
Save alexanderpp/9b63b0a242b4fc5f127aadd15278f4e3 to your computer and use it in GitHub Desktop.
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