25

我想检测运行我的 Android 应用程序的设备有多快?

在 Android 上是否有任何 API 可以做到这一点?还是我必须自己进行基准测试?

如果设备的 CPU 速度较慢,我想关闭一些耗时的操作,如动画或限制同时 HTTP 请求的最大数量。

4

4 回答 4

25

在我看来,最好的方法是监控执行这些操作所需的时间。如果花费太多时间,则系统太慢,您可以禁用花哨的功能,直到它足够快。

阅读 CPU 速度或其他规格并尝试判断系统速度是一个坏主意。未来的硬件变化可能会使这些规格变得毫无意义。

以 Pentium 4 与 Core 2 为例。2.4 GHz Pentium 4 或 1.8 GHz Core 2 哪个 CPU 更快?2 GHz Opteron 是否比 1.4 GHz Itanium 2 快?你怎么知道哪种 ARM CPU 实际上更快?

为了获得 Windows Vista 和 7 的系统速度评级,微软实际上对机器进行了基准测试。这是确定系统功能的唯一半准确方法。

看起来一个好方法是使用SystemClock.uptimeMillis()。

于 2011-02-02T15:42:38.410 回答
8

尝试读取/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
于 2011-02-02T14:22:25.227 回答
2

基于@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://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;
}
于 2015-01-07T14:28:46.000 回答
1

请参考以下链接,该链接将提供 CPU 相关数据

于 2011-02-02T14:31:06.117 回答