10

I'm currently going through the Project Euler's problems and I'm solving them both in C# and Scheme (the Racket implementation). I know C# but I'm learning Scheme as I go along.

Now, on top of having the correct results for the problems, I like to come up with the fastest possible solution I can think of. To that effect, in C# I use StopWatch to measure the performance of my code. Is there a similar Scheme library / functionality to time code execution?

joce
  • 9,624
  • 19
  • 56
  • 74
  • Which implementation of Scheme are you using? Such a function isn't part of the standard core Scheme, but would be provided as an additional function by whoever implemented your Scheme environment. – Greg Hewgill May 17 '11 at 22:11

1 Answers1

16

Just enclose the whole part you want to time in the time expression (this works in most implementations, including Racket):

(time (rest-of-program))

You can also use the Unix command time if you're on Linux/OSX/BSD/etc., e.g.

time ./my_program
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • 2
    Also, if you want to capture the times (e.g. to store them in a table or save them to disk) you can use time-apply, which returns multiple values. – John Clements May 17 '11 at 23:19
  • 1
    Rafe: This is *almost* what I want. From the documentation, I found it reports the milliseconds for execution http://docs.racket-lang.org/reference/time.html#(def._((quote._~23~25kernel)._time-apply)) . Is there a way to get to a finer time interval, like `StopWatch` does for C#? – joce May 18 '11 at 22:50
  • @Joce how much finer does it get? E.g. what unit does C# use? – Rafe Kettler May 18 '11 at 23:15
  • I use `ticks`. From the `StopWatch` documentation ( http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency.aspx), this is a machine dependent unit. On my machine, the frequency is equal to 2604160, thus a tick is roughly 384 nanoseconds, roughly 3 orders of magnitude smaller than a millisecond. Is there a timer that can get me that in Scheme? – joce May 19 '11 at 02:13