Reputation: 8670
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.
Upvotes: 26
Views: 31486
Reputation: 2773
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;
}
Upvotes: 2
Reputation: 5308
Kindly refer to the following link which will provide the CPU related data
Upvotes: 0
Reputation: 54325
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().
Upvotes: 24
Reputation: 274552
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
Upvotes: 9