pyret-lang icon indicating copy to clipboard operation
pyret-lang copied to clipboard

Add time-only and time-value functions to complement time-now

Open blerner opened this issue 1 year ago • 3 comments

We have time-now, that returns the current time in milliseconds since the epoch. We should add

time-only :: <T> (( -> T) -> Number)
time-value :: <T> ( -> T) -> {T; Number}

fun time-only(f) block:
  start = time-now()
  f()
  stop = time-now()
  stop - start
end

fun time-value(f) block:
  start = time-now()
  ans = f()
  stop = time-now()
  { ans ; stop - start }
end

that are more usable/informative.

blerner avatar Dec 14 '23 16:12 blerner

I would put a contract on these so that if someone were to accidentally pass in a non-thunk value they'll get a run-time error. I imagine a lot of students will write something like

time-only(some-big-computation())

and…not at all get what they expect. Of course they'll get an error at f() but a contract error may be better. (Ideally, they get an even more informative error that educates them, but that requires reflecting on value types and arity and what not.)

shriram avatar Dec 14 '23 17:12 shriram

I might swap the order of values in time-value so that time comes first and value comes second, both matching the focus and the function's naem.

shriram avatar Dec 14 '23 17:12 shriram

The first two lines of the code-snippet are contracts :)

I kept the order that (I think) @schanzer requested in the planning doc; if you want to swap it, that's trivial and fine by me.

blerner avatar Dec 14 '23 17:12 blerner

Where should these functions live? In a helper library? In essentials####? Somewhere else? time-now is defined by runtime itself, since it's a JS primitive, but these others are Pyret utility functions.

blerner avatar Apr 30 '24 17:04 blerner

Hm. I think I'd make a new module called timing. Re-export time-now and define and export these two?

jpolitz avatar May 01 '24 20:05 jpolitz