• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to measure little time intervals?

jgg

Lurker
Hi there!

I would like to make an app that measures little time intervals (nanoseconds) so I need to know about time resolution and precision regarding such measures with an Android device.

In other words:

- Is it feasible to get reliable values of the current time with nanosecond resolution?
- If so, is feasible to measure such little time interavals? I mean, is there any way to tell the app/OS to focus on measuring the time interval specified in the current thread instead of allowing other processes to make the measuring task not precise?

I've tried to measure the time between the execution of two different lines in code but the results are not always the same, somehow random, then I suppose Dalvik and its process management is causing this to be unreliable and random. What I need to do is to measure distances by getting transmission and reception of audio tones of 16-20 KHz (done in [1] with iOS), thus needing resolution and precision. I don't know if there is any way to get this working or do I need to get into JNI or what @_@.

Anyway, does anybody have any idea about this?

Thanks in advance!!

[1] http://blog.makezine.com/2011/11/14/iphone-acoustic-ruler/
 
Welcome to Android Forms jgg. I moved this to the Developer 101 forum so it gets better eyes on it.:)
 
In the System class, there is a method called nanoTime() that returns the current timestamp in nanoseconds. It is the highest resolution timer accessible in Java. With that said, it should only be used in comparison to another nanoTime() call's result. It should not be used as a system time expression. This should suffice in your case, though.

Here is the documentation on it:
System | Android Developers)
 
Thanks @Unforgiven and @jerofld, and sorry for the inconvenience. I didn't realize there was a specific developer section (not scrolling down enough, my bad!).

@jonbonazza, this is what I did use and gave me different results every time I executed (already using System.nanoTime()):

Code:
double syncTime;
double pulseTime;

float increment = (float)(2*Math.PI) * PULSE_FREQUENCY / 44100;
float angle = 0;
float samples[] = new float[PULSE_DURATION * 44100];


syncTime = System.nanoTime()/1000000.0;

for( int i = 0; i < samples.length; i++ )
{
	samples[i] = (float) Math.sin(angle);
	angle += increment;
}

pulseTime = System.nanoTime()/1000000.0;

activity.updateDetectedLabel(String.valueOf(pulseTime - syncTime));

The last line of code involves UI update, which I run on a the specific UI thread. However, what I do to check the time to complete the previous loop is to print or watch it, so UI does not have any influence. The result, as I said before: different values every time I execute the code, being one result many seconds far from the other.

Maybe I'm doing something wrong, but I can't see it as it is a simple substraction and I think I'm using the proper types.
 
Back
Top Bottom