A simple method is to just use millis(), which will give you the time from sketch start in milliseconds. This doesn't give you nanoseconds, but should be fine for many applications.
To compute a time difference, just save millis() to a variable on an event -- then compare that variable to the current millis(), e.g.:
int lastTime = 0;
int delta = 0;
void draw(){
delta = millis() - lastTime;
println(delta);
// draw code here
lastTime = millis();
}
A simple example of a timed action is to test how long a key was held down:
void draw(){
// draw code here
}
void keyPressed(){
int time1 = millis();
}
void keyReleased(){
int time2 = millis() - time1;
println(time2)
}
@poisson86 -- re:"That's the same thing" -- no, I don't believe that milliseconds and nanoseconds are the same thing -- from discussion I've seen I believe that System.nanoTime is different, e.g.
nanoTime is usually significantly more accurate than currentTimeMillis but it's a relatively expensive call as well.currentTimeMillis() runs in a few (5-6) cpu clocks, nanoTime depends on the underlying architecture and can be 100+ cpu clocks.
Answers
I'm afraid those delta nano time calculations are done internally via local variables: :(
https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L2379
Most we can access is the
protected
field frameRateLastNanos.Which is nothing more than the previous System.nanoTime():
https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L2445
There's also field frameRate:
https://Processing.org/reference/frameRate.html
Dunno how useful that'll be for ya, but here's some test I did now: 8-|
I'm checking it, deltaTime is used for not base a program on processor power
you can maybe use millis() or access the underlying java millisecond and "nanosecond" timers.
http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime
if you set the framerate as high as possible then it'll effectively disable the 60fps 'ticks' that are the default.
What are "1e3" and "1e6d" ??
1000 and 1000000
engineering notation for numbers, used to confuse people.
https://en.wikipedia.org/wiki/Engineering_notation
the d in 1e6d is to denote a double precision floating point number.
Okay :) didn't know about this
A simple method is to just use
millis()
, which will give you the time from sketch start in milliseconds. This doesn't give you nanoseconds, but should be fine for many applications.To compute a time difference, just save millis() to a variable on an event -- then compare that variable to the current millis(), e.g.:
A simple example of a timed action is to test how long a key was held down:
That's the same thing, millis or nanosecond ^^
you might need to use a long rather than an int for nanoTime(). and nanoseconds may be negative... see the javadoc for System.nanoTime()
https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#nanoTime()
@poisson86 -- re:"That's the same thing" -- no, I don't believe that milliseconds and nanoseconds are the same thing -- from discussion I've seen I believe that System.nanoTime is different, e.g.