This article explores different ways to measure the elapsed time of the program in Kotlin.

1. Using System.nanoTime() function

The recommended approach to measure the elapsed time of the program in Kotlin is with System.nanoTime(), which returns the current value of JVM’s time source with nanosecond precision.

Download Code

2. Using System.currentTimeMillis() function

You can also measure the elapsed time with System.currentTimeMillis(), but the results might not be that accurate.

Download Code

 
Here’s alternative solution using Instant.now() which internally uses System.currentTimeMillis().

Download Code

3. Using Date.getTime() function

The Date class also offers the getTime() function that returns the milliseconds’ number since epoch. We can also use this to measure elapsed time in Kotlin, as shown below:

Download Code

That’s all about measuring elapsed time in Kotlin.