Sunday, May 17, 2009

Android: millisecond resolution time

The only mention I could find of time in the android docs was android.text.format.Time. It has a tomillis() function that returns things in units of milliseconds, but the time only increments in values of 1 second, so that's pretty useless.

These guys mention android.os.SystemClock, which works for me:

package com.example.benchmark;

import android.os.Bundle;
import android.widget.TextView;
import java.util.Formatter;
import android.os.SystemClock;

public class BenchmarkActivity extends Activity
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)

long before = SystemClock.uptimeMillis();

long i, j=0;
for(i=0; i<23456780; i++) j+=i;

long after = SystemClock.uptimeMillis();

long millis = after - before;

Formatter f = new Formatter();

String foo = f.format("%d", millis).toString();

TextView tv = new TextView(this);


No comments: