简体   繁体   English

Android性能:Strings vs. Enums vs. Static Final Ints

[英]Android Performance: Strings vs. Enums vs. Static Final Ints

Parts of this have been asked on SO before, but I'm yet to find much in the way of solid evidence and/or a decisive answer. 之前已经问过部分内容,但我还没有找到可靠证据和/或决定性答案的方法。

When passing information around Android in a Bundle, what (if any) performance increases/decreases can be observed by using strings , enums or static final ints ? 在Bundle中传递Android周围的信息时,使用字符串枚举静态最终整数可以观察到(如果有的话)性能的增加/减少?

To give an example use case, there are several times in my app where a certain fragment is loaded and displayed. 举一个用例示例,我的应用程序中有几次加载并显示某个片段。 Each time the fragment is loaded, a Bundle is passed to it containing two arguments: an ID of the data is is showing, and a mode to dictate how to show it. 每次加载片段时,Bundle都会传递给它,其中包含两个参数:显示数据的ID,以及指示如何显示它的模式。 I have a custom state pager adapter set up doing all the heaving lifting, but the crux of the question is here: which (if any) of these would offer any performance gains/losses: 我有一个自定义状态寻呼机适配器设置完成所有提升,但问题的关键在于:这些(如果有的话)将提供任何性能增益/损失:

Bundle args = new Bundle();
args.putInt("ID", 1);

// method 1
args.putString("MODE", "Mode1");

// method 2
args.putSerializable("MODE", ModeEnum.Mode1);

// method 3
public static final int MODE_1 = 1; // this would be elsewhere in a constants class
args.putInt("MODE", MODE_1);

In the fragment, the mode is checked in several places, so I'm concerned with which of the three options is more efficient to store in the bundle, and which is more efficient to be compared on the other end. 在片段中,模式在几个地方被检查,所以我关心三个选项中的哪一个更有效地存储在包中,并且在另一端比较更有效。

Any advice, observations or experiences will be greatly appreciated! 任何建议,观察或经验将不胜感激!

Okay, so I did some research online and I found information that was either vague, unreliable or contradictory - in short, I didn't find an answer. 好吧,所以我在网上进行了一些研究,发现信息含糊不清,不可靠或矛盾 - 简而言之,我没有找到答案。 So instead, I set up a little app to actually test this for myself. 所以相反,我设置了一个小应用程序来实际测试这个。

The app passed 200 items in a bundle from one activity to another, then used those values for a mix of comparisons and lookups from arrays and maps. 该应用程序将一个活动中的200个项目从一个活动传递到另一个活动,然后将这些值用于比较和查找数组和地图的混合。 It then repeated this another 9,999 times. 然后再重复9,999次。

With 10k revolutions of this process, ran several times for each of the three methods, I came to a conclusion: 随着这个过程的10k转,对于这三种方法中的每一种都运行了几次,我得出了一个结论:

It doesn't really matter which one you pick. 你选择哪一个并不重要。

The differences in performance are tiny, and I mean miniscule . 在性能上的差异是很小的,我的意思是微乎其微 Strings lost the race most of the time by a slim margin, but ints and enums were both top contenders, and sometimes strings beat them both anyway. 大部分时间里,弦乐队都以微弱的优势输掉了比赛,但是整体和队型都是顶级的竞争者,无论如何有时弦乐都击败了他们。

The differences in memory consumption were just as narrow. 内存消耗的差异也很小。 As long as your string values aren't paragraphs of text, the differences are honestly too small to care about. 只要您的字符串值不是文本的段落,这些差异实际上太小而无法关注。 We're talking an increase of a few percent. 我们说的是增加了几个百分点。

Summary: 摘要:

Sure, if your app is passing around thousands of Bundle values all the time*, then it might be worth optimising it with ints or enums (whichever comes most naturally to your codebase). 当然,如果您的应用程序始终传递数千个Bundle值*,则可能需要使用整数或枚举(以您的代码库最自然的方式)对其进行优化。 But otherwise, any increase you get simply isn't worth the effort it would take to create it. 但除此之外,您获得的任何增加都不值得创建它所需的努力。

*If you're doing that, you probably know about this already, or there's something wrong in your app's infrastructure! *如果您这样做,您可能已经知道这一点,或者您的应用程序基础架构出现了问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM