brownplt / pyret-lang

The Pyret language.
Other
1.07k stars 111 forks source link

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

Closed blerner closed 6 months ago

blerner commented 11 months ago

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.

shriram commented 11 months ago

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 commented 11 months ago

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.

blerner commented 11 months ago

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 commented 6 months ago

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.

jpolitz commented 6 months ago

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