Inline functions help your program run faster by reducing extra steps when calling small functions.
Inline functions and performance in Kotlin
inline fun functionName(parameters) { // function body }
The inline keyword tells Kotlin to copy the function code directly where it is called.
This avoids the usual function call overhead.
inline fun greet(name: String) { println("Hello, $name!") }
inline fun operate(x: Int, y: Int, operation: (Int, Int) -> Int): Int { return operation(x, y) }
This program uses an inline function measureTime to measure how long a block of code takes to run. The block sums numbers from 1 to 1,000,000.
inline fun measureTime(block: () -> Unit) { val start = System.currentTimeMillis() block() val end = System.currentTimeMillis() println("Time taken: ${end - start} ms") } fun main() { measureTime { var sum = 0L for (i in 1..1_000_000) { sum += i } println("Sum: $sum") } }
Inline functions are best for small functions to avoid code size increase.
Using inline with large functions can make your program bigger.
Inline functions improve performance especially when using lambdas.
Inline functions copy code to where they are called to run faster.
They reduce the overhead of function calls and lambda objects.
Use them for small, frequently called functions to improve speed.
Practice
inline functions in Kotlin?Solution
Step 1: Understand what inline functions do
Inline functions copy their code directly to the place where they are called, avoiding the usual function call overhead.Step 2: Identify the main benefit
This copying reduces the overhead of calling small functions repeatedly, improving performance.Final Answer:
They reduce the overhead of function calls by copying code at call sites. -> Option DQuick Check:
Inline functions reduce call overhead [OK]
- Thinking inline makes functions asynchronous
- Believing inline optimizes memory for large data
- Confusing inline with Java interoperability
Solution
Step 1: Recall Kotlin inline function syntax
The keywordinlinemust appear beforefunin the function declaration.Step 2: Check each option
inline fun myFunction() { println("Hello") } correctly placesinlinebeforefun. Others place it incorrectly or inside the function body.Final Answer:
inline fun myFunction() { println("Hello") } -> Option BQuick Check:
inline keyword before fun [OK]
- Placing inline after fun keyword
- Putting inline inside function body
- Using inline as a function modifier incorrectly
inline fun greet(name: () -> String) {
println("Hello, ${name()}")
}
fun main() {
greet { "Alice" }
}Solution
Step 1: Understand the inline function call
The functiongreettakes a lambda returning a String and prints "Hello, " plus the lambda result.Step 2: Evaluate the lambda passed
The lambda returns "Alice", so the print statement outputs "Hello, Alice".Final Answer:
Hello, Alice -> Option AQuick Check:
Lambda returns "Alice" so output is Hello, Alice [OK]
- Confusing lambda with string literal
- Expecting compilation error from inline
- Printing lambda type instead of result
inline fun calculate(operation: () -> Int) {
return operation()
}
fun main() {
val result = calculate { 5 + 3 }
println(result)
}Solution
Step 1: Check function signature and return usage
The functioncalculateusesreturn operation()but does not declare a return type.Step 2: Kotlin requires explicit return type if function returns a value
Sinceoperation()returns Int,calculatemust declare: Intas return type.Final Answer:
The function calculate should specify a return type. -> Option CQuick Check:
Function returns Int but lacks return type [OK]
- Assuming inline forbids return statements
- Ignoring missing return type causes error
- Thinking lambda return type is restricted
Solution
Step 1: Understand inline with multiple lambdas
Inlining copies code of lambdas to call site, but lambdas markednoinlineare not inlined and keep normal object overhead.Step 2: Identify which lambdas to inline or not
To avoid creating lambda objects for the success lambda, it should be inlined. If failure lambda is large or stored, mark itnoinlineto avoid code bloat.Step 3: Check options
inline fun process(success: () -> Unit, noinline failure: () -> Unit) { success() failure() } correctly marksfailureasnoinlineandsuccessas inline, improving performance while controlling code size.Final Answer:
inline fun process(success: () -> Unit, noinline failure: () -> Unit) { success() failure() } -> Option AQuick Check:
Use noinline for lambdas not to inline [OK]
- Inlining all lambdas causing code bloat
- Not using noinline for stored lambdas
- Forgetting to mark function inline
Ready to prove it?
Pass the test and this pattern moves unknown
