Created
July 9, 2019 11:49
-
-
Save alexanderpp/9b63b0a242b4fc5f127aadd15278f4e3 to your computer and use it in GitHub Desktop.
Kotlin issue with iterating through ranges
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
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