# Millisecond precision over internet

**URL:** <https://www.ruby-forum.com/t/millisecond-precision-over-internet/58803>\
**Category:** Rails\
**Created:** [April 26, 2006, 1:45am UTC](https://www.ruby-forum.com/t/millisecond-precision-over-internet/58803 "2006-04-26T01:45:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aqaudhere](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@aqaudhere](https://www.ruby-forum.com/u/aqaudhere)\
**Post date:** [April 26, 2006, 1:45am UTC](https://www.ruby-forum.com/t/millisecond-precision-over-internet/58803/1 "2006-04-26T01:45:12Z")

</div>

Hi,

I’m new to programming and ruby and ROR, but I’m hoping to learn ruby to  
create custom scientific software.

Mainly it involves measuring people’s reaction time to a series of  
words.

I’m positive that this is possible using plain ruby on a computer (cause  
this is BASIC territory right?). But I want to create a ROR app that can  
do the same thing. My question is, is it possible to write an app that  
can measure reaction time with millisecond-precision (± 1 ms), then  
output a series of results to an array to be mailed back to the server?

Any help would be appreciated.

Yours,  
Daisung.

---

<div class="post-metadata">

**Author:** ![aqaudhere](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@aqaudhere](https://www.ruby-forum.com/u/aqaudhere)\
**Post date:** [April 26, 2006, 2:45am UTC](https://www.ruby-forum.com/t/millisecond-precision-over-internet/58803/2 "2006-04-26T02:45:58Z")

</div>

You will not be able to do that over the network.

I think your best bet is to measure it on the client using javascript  
and then send the measurements to your server. Even using javascript I’m  
not sure if you will be able to obtain the desired precision.

Cheers,

Benigno UrÃ­a.

---

<div class="post-metadata">

**Author:** ![aqaudhere](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@aqaudhere](https://www.ruby-forum.com/u/aqaudhere)\
**Post date:** [April 26, 2006, 3:13am UTC](https://www.ruby-forum.com/t/millisecond-precision-over-internet/58803/3 "2006-04-26T03:13:28Z")

</div>

You can get millisecond precision with javascript. Try this:

The purpose of this is to measure the javascript takes to do the whole  
create a new Date object, then get the timestamp from it. I kept  
clicking refresh and didn’t get two different numbers once after  
clicking refresh about 50 times. This means javascript (on my  
processor at least, and I expect its the same in all modern  
processors) can do all this in way less than 1 millisecond, probably  
less than 0.02 milliseconds. You might ask if Javascript is any slower  
with reacting to the click of a button. Well maybe it is a little, but  
it certainly wouldn’t get anywhere near half a millisecond.

-Nathan

---

<div class="post-metadata">

**Author:** ![aqaudhere](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@aqaudhere](https://www.ruby-forum.com/u/aqaudhere)\
**Post date:** [April 26, 2006, 3:38am UTC](https://www.ruby-forum.com/t/millisecond-precision-over-internet/58803/4 "2006-04-26T03:38:05Z")

</div>

removed\_email\_address@domain.invalid wrote:

> You can get millisecond precision with javascript. Try this:  
> I don’t doubt that. I do doubt that you can get anything close to  
> millisecond accuracy.

> The purpose of this is to measure the javascript takes to do the whole  
> create a new Date object, then get the timestamp from it. I kept  
> clicking refresh and didn’t get two different numbers once after  
> clicking refresh about 50 times. This means javascript (on my  
> processor at least, and I expect its the same in all modern  
> processors) can do all this in way less than 1 millisecond, probably  
> less than 0.02 milliseconds. You might ask if Javascript is any slower  
> with reacting to the click of a button. Well maybe it is a little, but  
> it certainly wouldn’t get anywhere near half a millisecond.  
> Really? What guarantees that? There’s a hell of a lot of stages  
> between the hardware interrupt and the browser event reaching user code,  
> even before taking into account the MSN messenger sitting in the system  
> tray that just happens to grab the processor at the wrong moment…

---

<div class="post-metadata">

**Author:** ![aqaudhere](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@aqaudhere](https://www.ruby-forum.com/u/aqaudhere)\
**Post date:** [April 26, 2006, 5:10am UTC](https://www.ruby-forum.com/t/millisecond-precision-over-internet/58803/5 "2006-04-26T05:10:03Z")

</div>

Well, ok, nothing guarantees it. I managed to devise a test which  
proves this. It works most of the time (returns zero, which is the  
ideal response), but about one in every 8 or so times on my computer,  
it returns a larger number such as 15. I thought it would go wrong  
less than that, and I also thought it wouldn’t be quite as wrong as  
that when it did. Guess I was over-optimistic.  
Please note that I’m well aware that my test doesn’t take into account  
the delay of the operating system in processing a mouse click, and it  
isn’t strictly true that a focus event is the same as a click event,  
but this test is the best I could come up with. It records the time  
when the page loads and focuses on the text box. The textbox then uses  
an onfocus event handler, which records the time again, and the first  
time is subtracted from the second.

---

<div class="post-metadata">

**Author:** ![aqaudhere](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@aqaudhere](https://www.ruby-forum.com/u/aqaudhere)\
**Post date:** [April 26, 2006, 10:30am UTC](https://www.ruby-forum.com/t/millisecond-precision-over-internet/58803/6 "2006-04-26T10:30:45Z")

</div>

Hi Daisung

if ruby can be installed on the client, you could write a client  
application  
which does the measure locally with all possible accuracy and precision,  
then have it push the informations to your RoR site (either using  
webservices, rpc, smtp or ftp). Actually this client application could  
be  
written in anything which suits your target audience (VB, .Net, Qt,  
SWT…).

This application could be packaged and available as a download from your  
RoR  
powered site.

hope this helps

## Thibaut

LoGeek  
[blog] [http://www.dotnetguru2.org/tbarrere](http://www.dotnetguru2.org/tbarrere)
