We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Processing millis() precision
Page Index Toggle Pages: 1
Processing millis() precision (Read 1253 times)
Processing millis() precision
Nov 9th, 2007, 12:13am
 
Ciao!

I'm running Processing 0125 on Win xp SP2.

Does anyone know how to get a better clock resolution than the one millis() returns?

on my system I just get milliseconds updates with a 15-16ms precision, run the code for an example:

//

int t0,t1,dt;

void setup(){
 

}

void draw(){
 
 dt=t1-t0;
 
 t0=t1;
 t1=millis();
 
 
 println(dt);

}

//output

0

16

0

16

15

16

16

15

16

31

0

16

15

32

...


as you can see the difference between the first time measurement t0 and the time measurement in the following frame t1 is in 15-16 ms steps.

it should depend on the system clock used by processing to read the millis() time. Is there a way to get a better precision like 1ms?

Thanks!

Re: Processing millis() precision
Reply #1 - Nov 9th, 2007, 12:28am
 
I think it is probably accurate, you need to take into account the fact that by default processing runs at 60 frames per second, and 1000/60=16.66 ms/frame.

Re: Processing millis() precision
Reply #2 - Nov 12th, 2007, 2:57am
 
thanks John, so the only way to get a better precision is to run at higher fps?

for example, I need something to happen PRECISELY 48 times every second.

so every 1000/48 = 20.83333333... ms something happens.

how to achieve this?

with the current millis() solution I can check if something has happened with a resolution of 16ms which is too low for what I need.

Re: Processing millis() precision
Reply #3 - Nov 12th, 2007, 4:26am
 
You really need System.nanoTime(), which has much higher resolution (though its accuracy is not guaranteed by the Java specs).  Alas, it first appeared in Java 1.5, whereas Processing is sticking to 1.4 compliance, so...

I'm not sure what happens if you just use it anyways, you could give it a shot.  Otherwise, you can always do it in Eclipse or Netbeans - Processing generates a .java file that you can use in one of those, though the project setup can be a bit tricky.
Re: Processing millis() precision
Reply #4 - Nov 12th, 2007, 11:34am
 
claudiomidolo wrote on Nov 12th, 2007, 2:57am:
for example, I need something to happen PRECISELY 48 times every second.

so every 1000/48 = 20.83333333... ms something happens.


You can try setting frameRate(48); which will do it's best to make the draw function run 48 times a second. However it is impossible to guarantee to get anything to run every 20.83 milliseconds, due to how your computer's task scheduling and multi-tasking works, the best you can get is approximately 48 times a second, ish.
Re: Processing millis() precision
Reply #5 - Nov 12th, 2007, 11:52pm
 
all this is about an audio sequencer I'm building... I will try to access System.nanoTime from a newer Java version.

If I use Processing expert it should access the Java version installed on my machine and not the default 1.4 is it correct?

Thanks!
Page Index Toggle Pages: 1