Subject: Re: High accuracy timer?
From: rpw3@rpw3.org (Rob Warnock)
Date: Wed, 27 Sep 2006 00:46:03 -0500
Newsgroups: comp.lang.lisp
Message-ID: <lJ-dnUbN39CGjYfYnZ2dnUVZ_rKdnZ2d@speakeasy.net>
<darkhedos@gmail.com> wrote:
+---------------
| Hello, I'm looking for an alternative to (get-internal-real-time) in
| Lisp to obtain more precise time intervals. get-internal-real-time
| seems to compare to GetTickCount on Win32 in term of accuracy, where
| it doesn't have any precision under 10-20 ms.
+---------------

Common Lisp does not define anything more precise than
GET-INTERNAL-REAL-TIME. And whether there even *is* anything
more precise also depends on your operating system, not just
your CL implementation. That said, however...

+---------------
| I haven't had luck finding any Lisp library dealing with this.
| There's | no implementation specific extension for that on my
| implementation (SBCL).
+---------------

Are you sure? SBCL was based on CMUCL, and CMUCL provides the
UNIX:UNIX-GETTIMEOFDAY function:

    cmu> (documentation #'unix:unix-gettimeofday 'function)

    "If it works, unix-gettimeofday returns 5 values: T, the seconds
       and microseconds of the current time of day, the timezone (in
       minutes west of Greenwich), and a daylight-savings flag. If it
       doesn't work, it returns NIL and the errno."
    cmu>

Note that the "seconds" here is *Unix* time in seconds, not CL's
"universal time". Add LISP::UNIX-TO-UNIVERSAL-TIME (2208988800)
to the former to get the latter.

    cmu> (unix:unix-gettimeofday)

    T
    1159335019
    692535
    420
    0
    cmu> (loop for i below 10 collect (nth-value 2 (unix:unix-gettimeofday)))

    (920407 920427 920435 920442 920449 920456 920462 920469 920476 920482)
    cmu> (mapcar #'- (cdr *) *)

    (20 8 7 7 7 6 7 7 6)
    cmu> 

Wow, that's slow! 6-8 us per iteration. Oh, yeah, I forgot,
that's interpreted. Let's try compiled:

    cmu> (funcall
	   (compile nil
	     (lambda ()
	       (loop for i below 10
		 collect (nth-value 2 (unix:unix-gettimeofday))))))
    ; Compiling LAMBDA NIL: 
    ; Compiling Top-Level Form: 

    (861686 861687 861688 861688 861689 861689 861690 861690 861691 861691)
    cmu> (mapcar #'- (cdr *) *)

    (1 1 0 1 0 1 0 1 0)
    cmu> 

That's better!!  ;-}  ;-}

Anyway, do an (APROPOS :GETTIMEOFDAY) in SBCL and see what you get...


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607