Timing accuracy

Ask questions and discuss programming issues around Tatool Java.
Locked
a.rey-mermet
Posts: 39
Joined: Fri Mar 07, 2014 6:21 am
Institution: University of Zurich

Timing accuracy

Post by a.rey-mermet » Tue Mar 18, 2014 7:27 am

Hi everybody!

I'm programming a stop-signal experiment and the timing does not seem accurate. Every stimulus is presented 2000 ms, and thus when no response is given, the durationTime should be 2000 ms (given that startTime starts with the presentation of the stimulus and endTime stops with the endTask() method). However, the durationTime varies from 2007 to 2024 ms. Why is there this variation? How can I avoid it?
I assume that this variation also occurs when a response is given. This suggests that the reaction times are increased. What do you think?

Cheers,
Alodie

alocher
Posts: 259
Joined: Tue Apr 24, 2012 12:56 pm
Institution: Tatool

Re: Timing accuracy

Post by alocher » Wed Mar 19, 2014 2:51 pm

Hi Alodie,

First of all it's of course important when you set your startTime and your endTime in your executable. The endTime should probably be set as soon as the respone has been given by the subject and not when you end your task. On the other hand the timing should probably start right after the stimulus has been made visible or just before that. On a modern operating system (e.g. Windows 7, Linux, Mac OS X) you should be getting a result that is up to 1ms correct. You should also make sure that no other applications are running which could impact the timing (see explanation further down).

Short answer:
If the analysis of your experimental data will depend on < 1 ms accurracy, you should probably look into using a different low-level hardware close programming language or even better specific hardware (not a normal pc or normal input devices) that are built to deliver accurate timing information.

Long answer (cited from our Tatool paper):
"Another possible limitation of Tatool concerns timing. Generally, the timing implementation in Java does not offer the same accuracy offered by programming languages that generate native machine code or external timing hardware. The accuracy of timing in Tatool relies heavily on the timer precision provided by the underlying operating system. The nanoTime method, available since Java 1.5, uses the highest resolution clock available on the platform, and while its return value is in nanoseconds, the update resolution is typically only in microseconds. On modern hardware and operating systems, the Java methods can deliver accuracy and precision in the microsecond range. Java’s timing performance can be improved by using the Java native interface, which calls functions provided by a low-level programming language such as C (cf. the implementation of the EEG trigger handler above)."

a.rey-mermet
Posts: 39
Joined: Fri Mar 07, 2014 6:21 am
Institution: University of Zurich

Re: Timing accuracy

Post by a.rey-mermet » Thu Mar 20, 2014 1:40 am

Hi Andre!

Thank you for your response. Do you have an example code or file in which the timing accuracy is improved as you suggest (i.e., by using the Java native interface, which calls functions provided by C)?

Cheers,
Alodie

alocher
Posts: 259
Joined: Tue Apr 24, 2012 12:56 pm
Institution: Tatool

Re: Timing accuracy

Post by alocher » Thu Mar 20, 2014 11:37 am

Unfortunately not, as we never had such a requirement.

a.rey-mermet
Posts: 39
Joined: Fri Mar 07, 2014 6:21 am
Institution: University of Zurich

Re: Timing accuracy

Post by a.rey-mermet » Fri Mar 21, 2014 1:58 am

And would it be possible to have the code of the EEG experiment in which the Java native interface calls functions provided by C?

alocher
Posts: 259
Joined: Tue Apr 24, 2012 12:56 pm
Institution: Tatool

Re: Timing accuracy

Post by alocher » Sun Mar 23, 2014 7:59 am

I went through the trouble to create a small JNI interface for C to access the QueryPerformanceCounter which is a high resolution timestamp. By the way the Java nanoTime() is using exactly the same function of the OS in the background. So I would expect the same output. On my Windows 7 machine the Java timing was accurate to 1 ms and so was the C timing. In my example code I set the timer function in java to 2000 ms and made sure my call for start time and end time where close to the timing start / stop. As you can also see from the log below this is not going to help you. The java timing has already been converted to ms but would of course also be available on sub ms level.

How to make sure your timing is as accurate as possible:
  • 1. Make sure you use a modern operating system which is able to give accurate timing (e.g. Windows 7+, Mac OS X, Linux).

    2. Make sure you don't have any other applications running which could affect the process priority of your application.

    3. Make sure you get the start and end time at the correct position within your code. The startTime is where you start measuring time (visibility of stimulus or start of timer) and the endTime is when a user triggers an answer or where the measuring should stop after a specific amount of time.
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.4310786277056
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.3021231293678
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.993753015995
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.5524485260248
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 2000.1236009597778
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 2000
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.4252778738737
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.224928304553
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.708623021841
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 2000.0972744077444
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 2000
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.4190308898687
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.4600824713707
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 2000.139218404889
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 2000
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.3953816145658
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 2000.0218644291162
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 2000
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.6644479483366
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.8763990402222
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.4368794113398
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.6586471796036
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 2000.0722864866257
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 2000
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 2001.567545697093
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 2001
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.2976609915495
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 2000.0205257982016
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 2000
DEBUG [AWT-EventQueue-0] (MyExecutable.java:265) - C / QueryPerformanceCounter: 1999.4886400848627
DEBUG [AWT-EventQueue-0] (MyExecutable.java:267) - Java / System.nanoTime() : 1999

Locked

Who is online

Users browsing this forum: No registered users and 1 guest