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

Nougat is much slower when compared to marshmallow

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

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

我不知道是什么原因造成的。我的一些想法如下:

感谢任何见解或复制!谢谢

主要活动

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>

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