Skip to content

Instantly share code, notes, and snippets.

@MarcinMoskala
Created April 7, 2018 09:21
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 MarcinMoskala/951a6faa5a5e6d044fea215b5be8c2a2 to your computer and use it in GitHub Desktop.
Save MarcinMoskala/951a6faa5a5e6d044fea215b5be8c2a2 to your computer and use it in GitHub Desktop.
Inline and noinline repeat
import kotlin.system.measureNanoTime
fun main(args: Array<String>) {
measureNanoTime { code1() }
measureNanoTime { code2() }
val tries = 100
var sum1 = 0L
var sum2 = 0L
for (i in 1..tries) {
println(i)
val time1 = measureNanoTime { code1() }
sum1 += time1
println("Time for code 1 = $time1")
val time2 = measureNanoTime { code2() }
println("Time for code 2 = $time2")
sum2 += time2
}
println("Sum time for 1 = $sum1")
println("Sum time for 2 = $sum2")
}
private fun code1() {
var a = 0
inlineRepeat(100_000_000) {
a += 1
}
}
fun code2() {
var a = 0
noinlineRepeat(100_000_000) {
a += 1
}
}
inline fun inlineRepeat(times: Int, action: (Int) -> Unit) {
for (index in 0 until times) {
action(index)
}
}
fun noinlineRepeat(times: Int, action: (Int) -> Unit) {
for (index in 0 until times) {
action(index)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment