English 中文(简体)
Is there an equivalent to Lisp s "runtime" primitive in Scheme?
原标题:

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?

最佳回答

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.

问题回答

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))

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
> 

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

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)))




相关问题
Lisp code called from Java

Long story: I am doing a project for my functional programing class, and I thought of writing an AI controller in Lisp, for the Mario AI competition. I was looking over frameworks/libraries/ways of ...

Emacs, Zen-Coding mode, and Putty

I use emacs via Putty and since Putty doesn t send certain key combinations to the remote console I generally need to re-bind them to other key combinations. After installing the amazing Zen-Coding ...

In Which Cases Is Better To Use Clojure? [closed]

I develop in Lisp and in Scheme, but I was reading about Clojure and then I want to know, in which cases is better to use it than using Lisp or Scheme? Thanks

lambda-gtk negative pointer

I was trying to write my own put-pixel on (Gdk) pixbuf in Lisp. When I finally realized how I can operate on C pointers in CL, new obstacle came along - (gdk:pixbuf-get-pixels pb) returns me negative ...

Is there a common lisp package naming convention?

I have created some of my own user packages and have run into a name clash. In Java, the naming convention is to use your domain name in the package name: e.g. import com.example.somepackage;. Are ...

SOAP request from within an AutoLISP/AutoCAD macro

We have built a webservice for a client that uses AutoCAD. They have a macro that runs in AutoCAD that builds a SOAP request. But they have not figured out how to actually send() the soap request to ...

热门标签