【问题标题】:How to detect android cpu speed?如何检测android cpu速度?
【发布时间】:2011-06-20 00:17:04
【问题描述】:

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

是否有任何 API 可以在 Android 上执行此操作?还是我必须自己进行基准测试?

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

【问题讨论】:

    标签: android performance detect cpu-speed


    【解决方案1】:

    尝试读取包含cpu信息的/proc/cpuinfo

       String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
       ProcessBuilder pb = new ProcessBuilder(args);
    
       Process process = pb.start();
       InputStream in = process.getInputStream();
       //read the stream
    

    【讨论】:

    • 这是一个非常糟糕的主意。我的意思是,它会工作。但是,当您的应用程序在未来版本的 CPU 上运行时,该版本会以较慢的时钟速度完成更多工作时会做什么?如果该应用程序在 Pentium 4s 上完成此操作,那么当 Core2 出现时,该应用程序将看起来很愚蠢。
    • 抱歉,我应该更多地向 darbat 发表评论。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      阅读 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 的系统速度评级,Microsoft 实际上对机器进行了基准测试。这是确定系统功能的唯一半准确方法。

      看起来不错的方法是使用SystemClock.uptimeMillis().

      【讨论】:

      • 我应该在 System.currentTimeMillis() 的帮助下计算时间吗?
      • @darbat: developer.android.com/reference/android/os/SystemClock.html 尝试使用 uptimeMillis()。
      • @ZanLynx:我正在尝试解决同样的问题,但根据我的经验,由于后台进程,CPU 基准测试在 Android 上非常不准确。您找到缓解这种情况的方法了吗?
      • @JonnyBoy:尽你所能。如果您可以通过定时重要操作来即时适应,那是最好的。许多 GUI 动画通过在系统太忙时跳过中间帧来做到这一点。
      【解决方案4】:

      基于@dogbane 解决方案和这个answer,这是我获得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;
      }
      

      【讨论】:

      • 我必须更改上面代码中的拆分正则表达式才能获得 bogoMIPS 的有效读数:String[] cpuInfoArray =cpuInfo.split(":|\\n"); Cheers
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多