34

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?

Eli Barzilay
  • 29,301
  • 3
  • 67
  • 110
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • I just came across exactly this problem :) – Chris Taylor Jul 08 '11 at 00:09
  • Interesting that I'm using Bill the Lizard's blog to check my answers as I'm going through SICP 4 years after he went through it. Did you see this coming when you asked this question, Mr. Lizard? – Cody Piersall Aug 31 '14 at 03:51
  • Here's a gist with an alternative `timed-prime-test` that works in `racket`: https://gist.github.com/Isaac-Kleinman/1b623bc2463b241b4383 – Isaac Kleinman Sep 17 '15 at 21:08

5 Answers5

25

current-milliseconds 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.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
Eli Barzilay
  • 29,301
  • 3
  • 67
  • 110
  • 5
    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 Feb 05 '10 at 02:32
  • 1
    It is `current-milliseconds`, not `current-millisecond`. I tried to edit the answer but SO requires a minimum of 6 characters in the edit. – bruno nery Nov 10 '11 at 18:41
6

I too came across this problem today. I am using DrRacket, as it seems to have superseded DrScheme. Though this is an old thread, I am adding my findings for anyone new who stumbles across this thread.

With R5RS (#lang r5rs) as selected language, add following two lines before the program to make it work

(#%require (only racket/base current-milliseconds))
(define (runtime) (current-milliseconds))
Lazerbeak12345
  • 309
  • 4
  • 19
patentfox
  • 1,436
  • 2
  • 13
  • 28
2

You can use the package sicp that contains definition of the runtime.

Here is the package description.

And here is the installation instructions:

  • Open the Package Manager: in DrRacket choose the menu File then choose Package Manager….

  • In the tab Do What I Mean find the text field and enter: sicp

  • Finally click the Install button.

Now you can call runtime and other procedures from SICP:

#lang sicp
(runtime)

An expected output would be like this:

1606611898030115
> 
Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29
1

I'm using mit-scheme to do SICP and have found that milliseconds is given by (real-time-clock) as per the user manual.

mwal
  • 2,803
  • 26
  • 34
1

If you have an implementation that is conformant with the latest specification of the Scheme programming language Revised7 Report on the Algorithmic Language Scheme (R7RS) published in 2013, such as Chibi-Scheme, you should now use the current-second time library which was standardised in R7RS:

(current-second) time library procedure

Returns an inexact number representing the current time on the International Atomic Time (TAI) scale. The value 0.0 represents midnight on January 1, 1970 TAI (equivalent to ten seconds before midnight Universal Time) and the value 1.0 represents one TAI second later. Neither high accuracy nor high precision are required; in particular, returning Coordinated Universal Time plus a suitable constant might be the best an implementation can do.

Some exercises of the book Structure and Interpretation of Computer Programs (SICP) expect a runtime procedure measured in microseconds to be available, so you can use this definition:

(define (runtime) (* 1000 (current-second)))
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
  • OP wanted _better_ resolution than ms; R7RS `jiffies` may work but are implementation-dependent. In any case: _"You should now use the `current-second`...."_ maybe for R7RS, but the question is also tagged [tag:racket]; Racket is not R7RS. Racket has `current-seconds` in `racket/base` (not `current-second` as in R7RS), and `current-milliseconds`. You still need to look to the implementation for good time functions. And many still use R6RS, in no small part because of how good Chez is. It is disappointing that the Scheme community was split over R6RS vs R7RS, but still a fact of life. – ad absurdum Oct 31 '22 at 23:31
  • @adabsurdum Thanks for the information, I was not aware that Racket was not R7RS-conformant. I have updated my answer. As far as I am concerned, I am using [Chibi-Scheme](https://github.com/ashinn/chibi-scheme) on macOS because it is R7RS-conformant and available on Homebrew. – Géry Ogam Nov 01 '22 at 10:43