25

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.

Dariusz Bacinski
  • 8,324
  • 9
  • 38
  • 47

4 Answers4

25

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().

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • Should I just count time with help of System.currentTimeMillis()? – Dariusz Bacinski Feb 03 '11 at 14:22
  • @darbat: http://developer.android.com/reference/android/os/SystemClock.html Try using uptimeMillis(). – Zan Lynx Feb 03 '11 at 15:10
  • @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? – Simplex Jan 16 '12 at 13:11
  • @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. – Zan Lynx Jan 16 '12 at 17:41
8

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
dogbane
  • 266,786
  • 75
  • 396
  • 414
  • 2
    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. – Zan Lynx Feb 02 '11 at 15:37
  • 2
    Sorry, I should have addressed my comment more to darbat. – Zan Lynx Feb 02 '11 at 15:43
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;
}
Community
  • 1
  • 1
ahmed_khan_89
  • 2,755
  • 26
  • 49
  • 3
    I had to change the split regex in the code above to get a valid reading for bogoMIPS: `String[] cpuInfoArray =cpuInfo.split(":|\\n");` Cheers – Andrew Aarestad Jun 08 '15 at 18:25
0

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

chiranjib
  • 5,288
  • 8
  • 53
  • 82