From 5961d4b408615d99af16e90b7314b775750e2ccf Mon Sep 17 00:00:00 2001 From: Edoardo Luppi Date: Wed, 31 Jan 2024 23:31:33 +0100 Subject: [PATCH] refactor: drop the slowest run --- .../benchmarks/ManualMySQLBenchmarks.kt | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/antlr-kotlin-benchmarks/src/commonTest/kotlin/org/antlr/v4/kotlinruntime/benchmarks/ManualMySQLBenchmarks.kt b/antlr-kotlin-benchmarks/src/commonTest/kotlin/org/antlr/v4/kotlinruntime/benchmarks/ManualMySQLBenchmarks.kt index 9c693d69..a47b8be1 100644 --- a/antlr-kotlin-benchmarks/src/commonTest/kotlin/org/antlr/v4/kotlinruntime/benchmarks/ManualMySQLBenchmarks.kt +++ b/antlr-kotlin-benchmarks/src/commonTest/kotlin/org/antlr/v4/kotlinruntime/benchmarks/ManualMySQLBenchmarks.kt @@ -54,6 +54,10 @@ class ManualMySQLBenchmarks { } private fun averageTimeMs(title: String, times: Int, action: () -> Duration): Double { + require(times > 3) { + "At least 4 iterations are required" + } + val durations = ArrayList(times) println("\nBenchmark: $title") @@ -68,12 +72,18 @@ class ManualMySQLBenchmarks { } private fun averageMs(durations: List): Double { - if (durations.isEmpty()) { - return 0.0 + require(durations.size > 3) { + "At least 4 durations are required" } - val sum = durations.fold(Duration.ZERO) { acc, duration -> acc + duration } - val duration = sum / durations.size + // Sort and drop the slowest run + val durationsMinusSlowest = durations.asSequence() + .sortedDescending() + .drop(1) + .toList() + + val sum = durationsMinusSlowest.fold(Duration.ZERO) { acc, duration -> acc + duration } + val duration = sum / durationsMinusSlowest.size return milliseconds(duration) }