Zulip Chat Archive
Stream: new members
Topic: running time is 0ms...?
Asei Inoue (Aug 30 2024 at 11:09):
import Lean
open Lean Elab Command Term Meta
elab "#time " stx:command : command => do
let start_time ← IO.monoMsNow
elabCommand stx
let end_time ← IO.monoMsNow
logInfo m!"time: {end_time - start_time}ms"
def naivePower (x n : Nat) :=
aux x n 1
where aux (x n acc : Nat) :=
match n with
| 0 => acc
| n + 1 => aux x n (acc * x)
/-
time: 490ms
-/
#time #eval naivePower 2 110000
def main : IO Unit := do
let start_time ← IO.monoMsNow
let result := naivePower 2 110000
let end_time ← IO.monoMsNow
IO.println s!"result: {result}"
IO.println s!"naive power: ({end_time - start_time} ms)"
-- time: 0 ms
#eval main
Asei Inoue (Aug 30 2024 at 11:10):
why #eval main
outputs time : 0ms
?
Henrik Böving (Aug 30 2024 at 11:20):
Most likely because the naivePower got lifted out of main and is being precomputed
Asei Inoue (Aug 30 2024 at 11:21):
Thank you. how to prevent this?
Simon Daniel (Aug 30 2024 at 17:04):
if you compute naivePower in IO it does not seem to lift it out of main (but somehow the timing is different to the one of #time ~137ms)
def naivePower (x n : Nat): IO Nat := ...
Last updated: May 02 2025 at 03:31 UTC