fun main() {
val executionTime = measureTimeMillis {
// Code block to measure execution time
for (i in 1..1_000_000) {
// Simulating some task to measure execution time
val result = i * i
}
}
println("Execution time: $executionTime ms")
}
measureTimeMillis
is a higher-order function in Kotlin used to measure the time taken for the execution of a specific code block.fun main() { ... }
is the entry point of the Kotlin program.val executionTime = measureTimeMillis { ... }
creates a variable executionTime
to store the elapsed time during the execution of the code block provided within the curly braces.for (i in 1..1_000_000) { ... }
is a loop iterating from 1 to 1,000,000, simulating a task to measure execution time.val result = i * i
performs a simple calculation to simulate a computational task within the loop.println("Execution time: $executionTime ms")
prints the elapsed time in milliseconds once the code block inside measureTimeMillis
completes execution.