d474f207e3
This patch optimizes the following pattern:
for (i in first..last step st) { ... }
In this case we need to create a Progression object and then call its
iterator() method causing at least 2 allocation per loop. This change
replaces such loops with the following constuction:
var inductionVar = first
checkProgressionStep(step) // check if step > 0
last = getProgressionLastElement(first, last, step)
if (first <= last) {
do {
i = inductionVar
inductionVar += step
...
} while(i != last)
}
54 lines
762 B
Kotlin
54 lines
762 B
Kotlin
fun main(args: Array<String>) {
|
|
|
|
// Simple loops
|
|
for (i in 0..4) {
|
|
print(i)
|
|
}
|
|
println()
|
|
|
|
for (i in 0 until 4) {
|
|
print(i)
|
|
}
|
|
println()
|
|
|
|
for (i in 4 downTo 0) {
|
|
print(i)
|
|
}
|
|
println()
|
|
println()
|
|
|
|
// Steps
|
|
for (i in 0..4 step 2) {
|
|
print(i)
|
|
}
|
|
println()
|
|
|
|
for (i in 0 until 4 step 2) {
|
|
print(i)
|
|
}
|
|
println()
|
|
|
|
for (i in 4 downTo 0 step 2) {
|
|
print(i)
|
|
}
|
|
println()
|
|
println()
|
|
|
|
|
|
// Two steps
|
|
for (i in 0..6 step 2 step 3) {
|
|
print(i)
|
|
}
|
|
println()
|
|
|
|
for (i in 0 until 6 step 2 step 3) {
|
|
print(i)
|
|
}
|
|
println()
|
|
|
|
for (i in 6 downTo 0 step 2 step 3) {
|
|
print(i)
|
|
}
|
|
println()
|
|
println()
|
|
} |