In addition to
current-second, the R7RS draft adds a timer function,
current-jiffy. Obviously the Theoretical Right Thing for a timer is a high-resolution version of the same linear timescale used for dates, which
current-second can (in an ideal implementation) provide. But in some applications (especially the rather important one of profiling), the timer can be called very frequently, so it must avoid allocation, which means either returning a fixnum or modifying some sort of mutable timer object. This is the point of
The main use case is something like this:
;;; Evaluate the body. Return multiple values: the time taken in ;;; seconds, followed by whatever values the body returned. ;;; This returns the time in seconds, not jiffies, for human ;;; readability. It returns the time as a number, rather than ;;; printing it somewhere, so it can be used programmatically.
(define-syntax time (syntax-rules () ((time forms ...) (let ((start (current-jiffy))) (let-values ((vals (begin forms ...))) (apply values (/ (mod (- (current-jiffy) start) jiffy-modulus) (jiffies-per-second)) vals))))))
(time (/ (factorial 100) (factorial 99)))0.0124525432 100 ;hopefully
Presumably R7RS-large will include such a macro. (
bench?) But in the current R7RS-small, users can't portably write it, or do anything else with jiffies reliably, because they can't deal with overflow.
High-precision fixnum timers overflow fast. A 32-bit microsecond clock, for instance, overflows every 71 minutes, and a 30-bit fixnum every 17 minutes. This is too frequent to ignore, so all subtraction and comparison of jiffies needs to be modulo the word size, so all programs using
current-jiffy need to know
jiffy-modulus. In theory they could determine it by experiment, but that takes a while and is imprecise, so in practice portable code can't use
current-jiffy for anything but setting random seeds.
This could be fixed by adding
max-jiffies?) to the standard. But since
current-second already covers timing fairly well, it might be better to leave
current-jiffy to R7RS-large. Efficient timers are a useful feature, but not one that belongs in Small Scheme.
By the way, shouldn't
jiffies-per-second be a constant, not a function? It's in the section called “Standard Procedures”, which does not describe a numeric constant, but it's better to change the title of the section (Standard Definitions? Standard Procedures and Constants?) than to change the language to fit the title.
1IIUC it originally had another point: to provide a linear timescale even if
current-second didn't. But now that
current-second is supposed to provide TAI,
current-jiffy is no longer needed for this, and even if
current-second only provides disguised POSIX time, its nonlinearities are infrequent enough for most applications to ignore.