与棉花糖相比,牛轧糖的速度要慢得多

时间:2017-03-02 18:47:02

标签: java android performance android-6.0-marshmallow android-7.0-nougat

我一直在制作一款安卓游戏,我注意到它在Nougat上的运行速度比在Marshmallow上慢两倍。事实上,我发现在我的代码中的任何两点之间它似乎有大致相同的时间增加。

所以我做了一个测试应用程序,当按下后退按钮时,进行测试,但我再次得到大致相同的结果(代码如下)。我使用两个Android虚拟设备对此进行了测试(请参阅代码记录的平均时间,以毫秒为单位),但我最初注意到它,因为我的平板电脑已更新为牛轧糖,因此它不是特定于AVD的。

我对导致这种情况的原因感到难过。我有一些想法如下:

  • 编译器没有针对nougat进行优化(api min为15,目标为25,我正在使用sdk工具25.0.2构建)。似乎不太可能,因为这将是一个非常普遍的问题,谷歌会立即修复。
  • 操作系统正在使用更多资源,因此我的游戏剩余的更少。不太可能,因为这不应该导致速度降低2倍。
  • 为CPU使用量添加较小的操作系统。也许我必须要求高优先级或类似的东西,但我期望明确的文档,所以也不太可能。
  • Android studio的设置对我来说不是最佳选择。这个似乎对我最有可能,但我找不到任何有用的东西。

任何见解或复制品都表示赞赏!感谢。

MainActivity

package com.example.nickgkg.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
  TextView text;
  long time = 0;
  @Override
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView)this.findViewById(R.id.text);
  }
  @Override
  public void onBackPressed(){
    //API 24(nougat) vs API 23(marshmallow)
    //API 25 performs the same as API 24 so I didn't include it
    long t=System.nanoTime();
    if(false){//Toggle this to change the test
      float garbage;
      for(int i=0;i<100000000;i++)
        garbage=i*27/2200f;
      //API 24:710ms vs API 23:620ms
    }else{
      for(int i=0;i<1000000;i++)
        something();
      //API 24:320ms vs API 23:120ms
    }
    time=System.nanoTime() - t;
    text.setText(time/1000000f+"ms");
  }
  private void something(){
    //BLANK function call
  }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.nickgkg.test.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/text"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

更新android studio修复了这个问题 (从2.2更新到2.3)。我仍然不确定为什么会发生这种情况,但我认为可能是nougat的调试版本没有像marshmallow的调试版本那样进行优化。另一个注意事项;也许如果我使用发布版本而不是调试版本进行测试,那么我将永远不会遇到这个问题(虽然没有测试过)。