Files
kotlin-fork/backend.native/tests/codegen/basics/for_loops.kt
T
Ilya Matveev d474f207e3 backend: Don't create a Progression objects in 'for' loops
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)
}
2017-07-18 17:05:58 +07:00

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()
}