我在until下面的循环中使用了infix fun
for (x in 0 until bodies.size) bodies[x]()
Run Code Online (Sandbox Code Playgroud)
在使用YourKit分析我的代码时,我注意到我有大量的IntRange对象(大约2k/sec).

当我切换循环以int...int直接使用rangeTo时,它不会产生任何垃圾.
for (x in 0..bodies.size-1) bodies[x]()
Run Code Online (Sandbox Code Playgroud)
有人可以解释这两者之间的区别吗?从我所能说的Int.until只是回归this .. to
public infix fun Int.until(to: Int): IntRange {
val to_ = (to.toLong() - 1).toInt()
if (to_ > to) throw IllegalArgumentException("The to argument value '$to' was too small.")
return this .. to_
}
Run Code Online (Sandbox Code Playgroud)
在当前版本v1.0.4中,编译器优化了调用rangeTo和downTo函数,因为它们是for循环中最常见的.
我想他们很快就会优化until一段时间.
以下是相关的发行票:https://youtrack.jetbrains.com/issue/KT-9900.随意投票.
| 归档时间: |
|
| 查看次数: |
909 次 |
| 最近记录: |