我想检测我的Android应用程序运行的设备速度有多快?
在Android上是否有任何API可以实现此功能?还是我必须自己进行基准测试?
如果设备的CPU速度较慢,我希望关闭一些耗时操作,如动画或限制同时进行的最大HTTP请求次数。
我想检测我的Android应用程序运行的设备速度有多快?
在Android上是否有任何API可以实现此功能?还是我必须自己进行基准测试?
如果设备的CPU速度较慢,我希望关闭一些耗时操作,如动画或限制同时进行的最大HTTP请求次数。
在我看来,最好的方法是监控执行这些操作所需的时间。如果花费的时间过长,则系统太慢了,您可以在它变快之前禁用高级功能。
读取CPU速度或其他规格并试图判断系统速度是一个坏主意。未来硬件的更改可能会使这些规格毫无意义。
以 Pentium 4 和 Core 2 为例。哪个 CPU 更快,2.4 GHz 的 Pentium 4 还是 1.8 GHz 的 Core 2?2 GHz 的 Opteron 是否比 1.4 GHz 的 Itanium 2 更快?你怎么知道哪种 ARM CPU 实际上更快?
为了获取 Windows Vista 和 7 的系统速度评级,微软实际上对机器进行基准测试。这是唯一半准确的确定系统功能的方法。
看起来一个好的方法是使用SystemClock.uptimeMillis()。
尝试阅读/proc/cpuinfo
,其中包含CPU信息:
String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
ProcessBuilder pb = new ProcessBuilder(args);
Process process = pb.start();
InputStream in = process.getInputStream();
//read the stream
基于@dogbane的解决方案和这个答案,这是我获取BogoMIPS值的实现:
/**
* 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://dev59.com/KnA75IYBdhLWcg3w899J#3021088}
*
* @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;
}
String[] cpuInfoArray =cpuInfo.split(":|\\n");
谢谢。 - Andrew Aarestad