[R] Timer in R?

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Aug 11 15:34:25 CEST 2003


Jim Lemon <bitwrit at ozemail.com.au> writes:

> Johnathan Williams wrote:
> 
> > Would anyone be so kind as to write a routine to time 
> > mouse button presses in R to the nearest millisecond?
> >
> > If R had a timer of this kind and a few basic screen
> > handling routines (to write characters or graphics of
> > different sizes and colours at precise times) then it
> > would make it easy to use it for on-line data analysis
> > of data to control EEG experiments.
> 
> I do this under DOS for a battery of human performance tests. However, it 
> requires taking over the system clock _and_ the keyboard interrupt to time 
> keypresses to the nearest millisecond (and you can get a bit more than 
> that if you want). If anyone knows about how to do this under Linux, I 
> would love to port these tests over.

In principle, you can do this with the tcltk package, but I'm not sure
about the granularity. On my system,

> system.time(z<-sapply(1:1000,function(i)as.integer(.Tcl("clock clicks -milliseconds"))))
[1] 0.39 0.01 0.39 0.00 0.00

so there's an overhead of about 0.4 ms per time measurement. However,

> table(diff(z))

  0   1   6   7  19
618 378   1   1   1

so there appears to be some rather large delays introduced
occasionally (I've seen up to 33 ms). Most likely this is OS and
windowmanager dependent, and switching to a simpler WM could give
better results (e.g. fvwm or maybe even evilwm). Also, for
timing-critical operations, one should probably minimize any
background processing.

It seems to be necessary to use .Tcl(...) rather than the less
efficient (but structurally nicer) tkcmd(), at least for now (I'm
working on changes that should improve the efficiency). 

Binding a timing command to a button event is quite simple.

A different tack would be to wedge into the X event handling. The
XButtonEvent structure has a time field which is also of millisecond
resolution, 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list