views:

321

answers:

1

According to SICP section 1.2.6, exercise 1.22:

Most Lisp implementations include a primitive called runtime that returns an integer that specifies the amount of time the system has been running (measured, for example, in microseconds).

I'm using DrScheme, where runtime doesn't seem to be available, so I'm looking for a good substitute. I found in the PLT-Scheme Reference that there is a current-milliseconds primitive. Does anyone know if there's a timer in Scheme with better resolution?

+3  A: 

current-millisecond is a function that returns the current millisecond count from the system, but it might decrease. current-inexact-milliseconds is similar, but returns a guaranteed-to-increase floating point number.

There are also a bunch of similar functions that you can find on that page, but if all you need is to time a certain function, then just use (time expr) and it will print out the time it took to evaluate the expression.

Another thing that is relevant here is the profiler, in case you need some more verbose analysis of your code.

Eli Barzilay
I guess when the book was written a millisecond timer was precise enough. :) I'll just have to increase my input size to get some measurable timing results. Thanks for giving a variety of options, that's really helpful.
Bill the Lizard
That's wrong. First, there's no direct relation between SICP and PLT. Second, the millisecond counter is coming from the OS, and since interrupt handlers are handled at the level of a few milliseconds, there's no point in a finer counter. The granularity is usually at ~20ms, not even 1ms. If you have some language with, for example, a nanosecond counter, you won't get any better results. Third, measuring runtime at the msec level is a bad idea, you need to repeat the evaluation enough times to get a substantial count, otherwise the runtime will be lost in the noise.
Eli Barzilay