26

I would like to detect how fast is the device on which my Android application is running?

Is there any API to do it on Android? Or do I have to benchmark it by myself?

If the device has slow CPU I would like to turn off some time consuming operations like animations or limit maximum number of simultaneous HTTP request.

4 Answers 4

24

The best way to do it in my opinion is to monitor the time it takes to do these actions. If it is taking too much time, then the system is too slow and you can disable fancy features until it is fast enough.

Reading the CPU speed or other specs and attempting to judge the system speed is a bad idea. Future hardware changes might make these specs meaningless.

Look at the Pentium 4 vs the Core 2 for example. Which is the faster CPU, a 2.4 GHz Pentium 4, or the 1.8 GHz Core 2? Is a 2 GHz Opteron faster than a 1.4 GHz Itanium 2? How are you going to know what kind of ARM CPU is actually faster?

To get system speed ratings for Windows Vista and 7 Microsoft actually benchmarks the machine. This is the only halfway accurate method to determine system capabilities.

It looks like a good method is to use SystemClock.uptimeMillis().

Sign up to request clarification or add additional context in comments.

4 Comments

Should I just count time with help of System.currentTimeMillis()?
@ZanLynx: I'm trying to solve the same problem, but in my experience CPU benchmarking is very inaccurate on Android due to background processes. Have you found a way to mitigate that?
@JonnyBoy: Just do the best that you can. If you can adapt on the fly by timing important operations, that is the best. A lot of GUI animation does this by skipping intermediate frames when the system is too busy.
9

Try reading /proc/cpuinfo which contains cpu information:

   String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
   ProcessBuilder pb = new ProcessBuilder(args);

   Process process = pb.start();
   InputStream in = process.getInputStream();
   //read the stream

2 Comments

This is a pretty bad idea. I mean, it'll work. But what will your app do when run on some future version of CPU that does more work at a slower clock speed? If the app had done this on Pentium 4s then when Core2 came out the app would look stupid.
Sorry, I should have addressed my comment more to darbat.
2

Based on @dogbane solution and this answer, this is my implementation to get the BogoMIPS value:

 /**
 * parse the CPU info to get the BogoMIPS.
 * 
 * @return the BogoMIPS value as a String
 */
public static String getBogoMipsFromCpuInfo(){
    String result = null;
    String cpuInfo = readCPUinfo();
    String[] cpuInfoArray =cpuInfo.split(":");
    for( int i = 0 ; i< cpuInfoArray.length;i++){
        if(cpuInfoArray[i].contains("BogoMIPS")){
            result = cpuInfoArray[i+1];
            break;
        }
    }
    if(result != null) result = result.trim();
    return result;
}

/**
 * @see {https://stackoverflow.com/a/3021088/3014036}
 *
 * @return the CPU info.
 */
public static String readCPUinfo()
{
    ProcessBuilder cmd;
    String result="";
    InputStream in = null;
    try{
        String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
        cmd = new ProcessBuilder(args);
        Process process = cmd.start();
        in = process.getInputStream();
        byte[] re = new byte[1024];
        while(in.read(re) != -1){
            System.out.println(new String(re));
            result = result + new String(re);
        }
    } catch(IOException ex){
        ex.printStackTrace();
    } finally {
            try {
                if(in !=null)
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
    return result;
}

1 Comment

I had to change the split regex in the code above to get a valid reading for bogoMIPS: String[] cpuInfoArray =cpuInfo.split(":|\\n"); Cheers
0

Kindly refer to the following link which will provide the CPU related data

1 Comment

Link-only answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.