Junior — Middle
When is it recommended to use an inline function in programming?
sobes.tech AI
Answer from AI
Inline functions are recommended to be used when you need to reduce the overhead of function calls, especially if the function is small and called frequently. In Android and Kotlin, this helps improve performance because the compiler inserts the function body directly at the call site, avoiding the cost of a jump.
For example, for small handler functions or lambdas passed as parameters, using inline allows avoiding the creation of additional objects and improves execution speed.
inline fun measureTime(block: () -> Unit) {
val start = System.currentTimeMillis()
block()
println("Time: ${System.currentTimeMillis() - start} ms")
}