Wink Saville’s Blog

December 10, 2007

Android – fast/precise timing routines for timing in the small

Filed under: Android, programming — wink @ 7:24 am

I wanted to time how fast different approaches might be in for dispatching to states in a state machine. At first I used android.os.SystemClock.uptimeMillis() but I thought there should be something more portable. I did a web search and found that in Java 5.0 System.nanoTime() was added and in fact found that it is implemented in Android so I abstracted away the time base:

static final long readTsc() { return System.nanoTime(); } static final double tscToSecs(long tsc) { return tsc / 1000000000.0; }

I also tested the speed of 10 in a row and when executing on the android emulator I get the following:

V/TestSm ( 586): s[0]=1376737118000 diff=0.000000ns
V/TestSm ( 586): s[1]=1376737159000 diff=41000.000000ns
V/TestSm ( 586): s[2]=1376737178000 diff=19000.000000ns
V/TestSm ( 586): s[3]=1376737196000 diff=18000.000000ns
V/TestSm ( 586): s[4]=1376737214000 diff=18000.000000ns
V/TestSm ( 586): s[5]=1376737231000 diff=17000.000000ns V/TestSm ( 586): s[6]=1376737249000 diff=18000.000000ns
V/TestSm ( 586): s[7]=1376737267000 diff=18000.000000ns
V/TestSm ( 586): s[8]=1376737285000 diff=18000.000000ns
V/TestSm ( 586): s[9]=1376737302000 diff=17000.000000ns

When I execute this on my Intel developement system, a 6600 @ 2.4GHz the following is seen:

Tests: s[0]=15759382032271 diff=0ns
Tests: s[1]=15759382033117 diff=846ns
Tests: s[2]=15759382033529 diff=412ns
Tests: s[3]=15759382033918 diff=389ns
Tests: s[4]=15759382034300 diff=382ns
Tests: s[5]=15759382034693 diff=393ns
Tests: s[6]=15759382035083 diff=390ns
Tests: s[7]=15759382035442 diff=359ns
Tests: s[8]=15759382035801 diff=359ns
Tests: s[9]=15759382036161 diff=360ns

Greater than 40 times faster on my Intel box than the android editor. That would be expected, but even 41us isn’t too bad compared to the 439us for the android.os.SystemClock.uptimeMillis().

I’ve also created a class so I can easily use this as I develop the state machine code:

package com.saville.debug.android; public final class Timing { public static final long readTsc() { return System.nanoTime(); } public static final double tscToSecs(long tsc) { return (tsc / 1000000000.0); } public static final double tscToMillis(long tsc) { return (tsc / 1000000.0); } public static final double tscToUsecs(long tsc) { return (tsc / 1000.0); } public static final double tscToNanos(long tsc) { return ((double)tsc); } }

4 Comments »

  1. Thanks! This is exactly what I’ve been looking for. I’m impressed that the emulator can come up with max 41 microsecond intervals.

    Comment by Hamsalad — January 2, 2010 @ 7:13 pm

  2. Hi,
    Im using System.nanoTime() as you suggested but the results that i get are less precise than those you get.. i get time measurements with 7 digits at most while in you example the the time measurements are 10 to 14 digits….
    Do you have an idea what can be the reason for that?

    Comment by tcp — June 14, 2010 @ 4:32 am

  3. Actually, as you can see from the difference values the resolution is only 1us so I think you’re seeing expected results.

    Comment by wink — June 14, 2010 @ 7:27 am

  4. on the emulator it is indeed us but the development system is giving results in ns… or may be i dont understand it right?
    as I understand the Android CPU is capable of 1.5GHz therefore I assume it is capable of ns on a real device

    Comment by tcp — June 15, 2010 @ 4:30 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress