Skip to content

Instantly share code, notes, and snippets.

@valentineRutto
Last active April 29, 2024 20:55
Show Gist options
  • Save valentineRutto/25e49450cdea20212e31cbb5ef342d69 to your computer and use it in GitHub Desktop.
Save valentineRutto/25e49450cdea20212e31cbb5ef342d69 to your computer and use it in GitHub Desktop.
Calculate function execution time
import kotlin.time.measureTime
import kotlin.time.measureTimedValue
fun main() {
val executionTime = measureTime{
val isFlooding= predictFloodingInNairobi(0.35,7000.0)
println("Will Nairobi Flood? $isFlooding ")
}
println("TimeTaken in millis: $executionTime ")
val (value, timeTaken) = measureTimedValue{
predictFloodingInNairobi(0.35,7000.0)
}
println("Will Nairobi Flood: $value, ")
println("TimeTaken in microseconds: $timeTaken, ")
}
private fun predictFloodingInNairobi(amountOfRain: Double, areaInSquareMeters: Double): Boolean {
val rainfallThresholdInMillimeters = 50.0
val areaThreshold =1000.0
val rainfallInMillimeters = amountOfRain * 1000.0
return rainfallInMillimeters > rainfallThresholdInMillimeters && areaInSquareMeters > areaThreshold
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment