From 725137c82f33794de0a75ef00f7e70cb460c914f Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Tue, 5 Sep 2023 01:49:19 -0700 Subject: [PATCH] tool: add support for measuring runtime of a program --- .../main/kotlin/gay/pizza/pork/tool/RunCommand.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tool/src/main/kotlin/gay/pizza/pork/tool/RunCommand.kt b/tool/src/main/kotlin/gay/pizza/pork/tool/RunCommand.kt index 406ee16..f710432 100644 --- a/tool/src/main/kotlin/gay/pizza/pork/tool/RunCommand.kt +++ b/tool/src/main/kotlin/gay/pizza/pork/tool/RunCommand.kt @@ -7,16 +7,29 @@ import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.types.path import gay.pizza.pork.evaluator.CallableFunction import gay.pizza.pork.evaluator.Scope +import kotlin.system.measureTimeMillis class RunCommand : CliktCommand(help = "Run Program", name = "run") { val loop by option("--loop", help = "Loop Program").flag() + val measure by option("--measure", help = "Measure Time").flag() val path by argument("file").path(mustExist = true, canBeDir = false) override fun run() { if (loop) { while (true) { + runProgramMaybeMeasure() + } + } else { + runProgramMaybeMeasure() + } + } + + private fun runProgramMaybeMeasure() { + if (measure) { + val time = measureTimeMillis { runProgramOnce() } + println("time taken: $time ms") } else { runProgramOnce() }