什么是快?一个intent.putExtras(捆绑使用字符串)或多个intent.putExtra(字符串)? [英] What is faster? One intent.putExtras(Bundle with Strings) or many intent.putExtra(String)?

查看:939
本文介绍了什么是快?一个intent.putExtras(捆绑使用字符串)或多个intent.putExtra(字符串)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是快?添加一串字符串值到捆绑然后添加到意图?或者只是增加值到意图使用 intent.putExtra()?或者没有它多大区别?

What is faster? Adding a bunch of string values to a bundle and then adding that to an intent? Or just adding the values to an intent using intent.putExtra()? Or doesn't it make much difference?

一个谷歌搜索给我的教程,但不是太多的答案。只是要求出于好奇,想知道这会影响性能,使用一个或另一个。 <一href=\"http://stackoverflow.com/questions/11900266/intent-putextrastring-bundle-vs-intent-putextrabundle\">This一靠近,但不回答,我想知道的。

A Google search gave me tutorials, but not much of an answer. Just asking out of curiosity, wondering if it would affect performance to use one or the other. This got close, but does not answer what I would like to know.

推荐答案

创建捆绑你自己,然后补充说,以一个意图应该会更快。

Creating Bundle on your own and then adding that to an intent should be faster.

根据<一href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.4_r1.2/android/content/Intent.java#Intent.putExtra%28java.lang.String,boolean%29\"相对=nofollow>来源$ C ​​$ C , Intent.putExtra(字符串,字符串)方法是这样的:

public Intent putExtra(String name, String value) {
    if (mExtras == null) {
        mExtras = new Bundle();
    }
    mExtras.putString(name, value);
    return this;
}

所以,它总是会首先检查捆绑mExtras 已经创建。这就是为什么它可能与字符串增加的大序列慢一点。 Intent.putExtras(捆绑)是这样的:

So, it will always check at first if Bundle mExtras was already created. That's why it's probably a bit slower with big sequence of String additions. Intent.putExtras(Bundle) looks like this:

public Intent putExtras(Bundle extras) {
    if (mExtras == null) {
        mExtras = new Bundle();
    }
    mExtras.putAll(extras);
    return this;
}

那么,它会检查如果(mExtras == NULL)只有一次,以后所有的值添加到内部捆绑mExtras Bundle.putAll()

So, it will check if (mExtras == null) only once and later add all the values to the internal Bundle mExtras with Bundle.putAll():

public void putAll(Bundle map) {
     unparcel();
     map.unparcel();
     mMap.putAll(map.mMap);

     // fd state is now known if and only if both bundles already knew
     mHasFds |= map.mHasFds;
     mFdsKnown = mFdsKnown && map.mFdsKnown;
 }

捆绑是由地图备份的HashMap 为precise),所以将所有字符串一次在该地图上应该也比一个加弦乐的速度更快。

Bundle is backed up by a Map (HashMap to be precise), so adding all Strings at once to this map should be also faster than adding Strings one by one.

这篇关于什么是快?一个intent.putExtras(捆绑使用字符串)或多个intent.putExtra(字符串)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆