AsyncLayoutInflater vs Anko, AnkoはLayoutXMLを殺すのか?
YokomakuさんがAsyncLayoutInflater
とLitho
のパフォーマンス比較を行っていました。
keithyokoma.hatenablog.com
ちょっと気になったので私もAsyncLayoutInflater
とAnko
の比較を行ってみました。
AsyncLayoutInflater? Anko?
AsyncLayoutInflater?
LayoutXMLのInflateを非同期で行います。
Anko?
Kotlin製のView DSLです。コンセプトはいくかありますが以下のような利点が。
- LayoutXMLのパースを行わないため高速
- コードでViewを組むのでTypeSafe
以前軽く記事にしています。 lvla.hatenablog.com
Sample Codes
サンプルコードはYokomakuさんのものをfalkさせていただきました。 github.com
AsyncLayoutInflater
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/scroll" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/text" tools:context="io.github.keithyokoma.asynclayout.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 10000 TextViews --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!"/> ... </LinearLayout> </ScrollView>
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val s = System.nanoTime() AsyncLayoutInflater(this).inflate(R.layout.activity_main, null) { view, _, _ -> setContentView(view) val e = System.nanoTime() Log.i("TIME", "${(e-s)} nano sec") } } }
Anko
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) measureNanoTime { val text = "Hello World!" scrollView { lparams(width = matchParent, height = matchParent) verticalLayout { for (i in 0..9999) { textView(text) } }.lparams(width = matchParent, height = wrapContent) } }.let { Log.i("TIME", "$it nano sec") } } }
Benchmark
AsyncLayoutInflater
- 2268048923 ns
- 2274749393 ns
- 2299624654 ns
- 2346767994 ns
- 2387729873 ns
Ave. 2315384167.4 ns (2315.38 ms)
Anko
- 1692746575 ns
- 1719153036 ns
- 1676609542 ns
- 1737803819 ns
- 1757240852 ns
Ave. 1716710764.8 ns (1716.71 ms)
AsyncLayoutInflater vs Anko
やはりXMLのパースを行わない分、Ankoのほうが速いです。
じゃぁAnkoの方がいいかというとそうでもないかなーと。もしかしたら(Ankoに比べたら)ちょっと遅いけどUI Threadを止めないAsyncLayoutInflaterのほうが良いケースもあるかもしれません。
そして、AnkoではXMLを書かないので当然Databindingを利用できません。