Summary - Performance
- Kotlin course
- Expert
- Lesson 42/47
- 5 min
Combine profiling, inline and Sequence to write an optimized copy function.
Summary - Performance
In this group you learned profiling, allocation, inline, Sequence and coroutine overhead. Now you will write a model that combines them: an optimized summary function.
Your perfSummary function brings together three core tools: call optimization with inline, lazy computation with Sequence, and measurement with profiling. In main you will call this function and print the result. This is the typical performance combination.
Combining: inline + Sequence + profiling
Now we combine all parts. inline lowers call cost; Sequence removes intermediate collections; profiling measures the result.
Example model: fun perfSummary(): String = "perf: inline + Sequence". This function summarises the combination; in main you write println(perfSummary()). In a real optimization these three are used together: the hot path is inlined, data flows as a Sequence, and the change is measured with JMH.
Practice Write perfSummary
Define fun perfSummary(): String. It should return "perf: inline + Sequence". In main, write println(perfSummary()).
Starter code
// add the perfSummary function
fun main() {
// print the performance summary
}Sample solution
fun perfSummary(): String = "perf: inline + Sequence"
fun main() {
println(perfSummary())
}Expected output
perf: inline + Sequence
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which trio is combined for performance?
Performance rests on three core tools.- val, var, const
- class, object, inheritance
- KMP, Compose, KSP
- inline + Sequence + profiling Correct answer
Explanation: inline, Sequence and profiling are the core of performance.
2 Complete the perfSummary string
fun perfSummary(): String = "____"
fun perfSummary(): String = "perf: inline + Sequence"
Explanation: This string summarises the performance combination.
3 Match the performance concepts
| Term | Meaning |
|---|---|
profiling |
Measurement |
allocation |
Heap allocation |
inline |
Call embedding |
Sequence |
Lazy stream |