Example usage for android.os Debug threadCpuTimeNanos

List of usage examples for android.os Debug threadCpuTimeNanos

Introduction

In this page you can find the example usage for android.os Debug threadCpuTimeNanos.

Prototype

public static long threadCpuTimeNanos() 

Source Link

Document

Get an indication of thread CPU usage.

Usage

From source file:io.realm.datastorebenchmark.MainActivity.java

/**
 * Verify that the timer mechanism is available.
 *//*from   www . ja v a2 s. c o m*/
private void checkCpuTimeNanos() {
    if (Debug.threadCpuTimeNanos() == -1) {
        throw new RuntimeException("Debug.threadCpuTimeNanos() doesn't work.");
    }
}

From source file:io.realm.datastorebenchmark.MainActivity.java

/**
 * Measure the resolution of the timer.//w  ww.j a  v a2 s.  c o  m
 * Any measurements close to this value is unreliable.
 */
private void measureTimerResolution(File outputFolder) {
    // see http://gamasutra.com/view/feature/171774/getting_high_precision_timing_on_.php?print=1
    long diff = 50000; // very large value
    for (int i = 0; i < 100; i++) {
        long end;
        long start = Debug.threadCpuTimeNanos();
        while (true) {
            end = Debug.threadCpuTimeNanos();
            if (end != start) {
                if (diff > (end - start)) {
                    diff = end - start;
                }
                break;
            }
        }
    }
    try {
        File file = new File(outputFolder, "timer");
        FileOutputStream fileOutputStream = new FileOutputStream(file, false);
        fileOutputStream.write(String.format(Locale.US, "%d\n", NUMBER_OF_ITERATIONS).getBytes());
        fileOutputStream.write(String.format(Locale.US, "%d\n", NUMBER_OF_OBJECTS).getBytes());
        fileOutputStream.write(String.format(Locale.US, "%d\n", diff).getBytes());
        fileOutputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}