简体   繁体   English

如何在Android上有效存储位集

[英]How to efficiently store bitsets on Android

My application requires me to store sets of bits along with some accompanying metadata on the Android platform (read only for now). 我的应用程序要求我在Android平台上存储位集以及一些附带的元数据(目前仅读)。 Now obviously I could just implement the Serializable interface, but I hear it's very slow on Android (I can imagine has to do something with the custom VM and compiler that makes such reflective features inefficient). 现在显然可以实现Serializable接口,但是我听说它在Android上非常慢(我可以想象必须对自定义VM和编译器执行某些操作,从而使此类反射功能效率低下)。 Should I: 我是不是该:

  1. Use Android's Parcel system which seems to be an "assisted" marshalling technique. 使用Android的Parcel系统,这似乎是一种“辅助”编组技术。
  2. Use a custom binary format (maybe BMP style with the header information stripped). 使用自定义二进制格式(可能是去除标题信息的BMP样式)。
  3. Store into an XML file manually, use XML parser to retrieve data. 手动存储到XML文件中,使用XML解析器检索数据。

Now from what I understand XML serialization or parcelization isn't really backwards compatible on Android? 现在,据我了解,XML序列化或组合化在Android上实际上不是向后兼容的吗? The appeal of XML is of course that these persistent files could be edited in a regular text editor. XML的吸引力当然是可以在常规文本编辑器中编辑这些持久文件。 Which leaves me in a difficult position, since I hate writing code that is redundant. 由于我讨厌编写多余的代码,这使我处于困境。

At this point I am heavily leaning towards the first option (ie bitset being parcelized). 在这一点上,我非常倾向于第一种选择(即位集被打包)。 Any experienced Java/Android programmers care to tell me how well I can expect this to perform? 任何经验丰富的Java / Android程序员都在乎告诉我,我期望它执行得如何? Would I have to expand the bitset into an array of booleans to get acceptable runtime performance? 我是否必须将位集扩展为布尔数组才能获得可接受的运行时性能? Of course the problem with this is that even a rudimentary benchmark would have to be run on the Dalvik VM since I can't expect Sun's VM on x86 have similar performance to Android on ARM. 当然,这样做的问题是,甚至必须在Dalvik VM上运行基本的基准测试,因为我不能期望x86上的Sun VM具有与ARM上的Android类似的性能。 How does the Android emulator work? Android模拟器如何工作? Is it a VM on top of the x86 host or does it emulate the ARM instruction set and run the VM targeted at ARM? 它是x86主机之上的VM,还是模仿ARM指令集并运行针对ARM的VM?

I hope this ADD post didn't confuse everyone because it confused me. 我希望这篇ADD帖子不要让所有人感到困惑,因为它使我感到困惑。 :D :d

you heard a rumor that serialization is slow, so you're going to use XML? 您听说过串行化速度很慢的谣言,因此您将使用XML? lol. 大声笑。

you need to write yourself some realistic benchmarks, serializing the kinds of bitsets you actually need to deal with (large versus small, dense versus sparse, and so on). 您需要编写一些实际的基准,对您实际需要处理的比特集的类型进行序列化(大型与小型,密集与稀疏等)。 i strongly recommend http://code.google.com/p/caliper/ for writing your benchmarks. 我强烈建议http://code.google.com/p/caliper/编写基准测试。 http://code.google.com/p/vogar/ knows how to run a caliper benchmark on an Android device. http://code.google.com/p/vogar/知道如何在Android设备上运行卡尺基准测试。

as i say in Designing for Performance , emulator behavior is nothing like device behavior when it comes to performance. 正如我在说性能设计 ,仿真器的行为是一样的器件行为,当涉及到性能。 you need to test on the lowest-performance device you actually care about. 需要在实际关心的性能最低的设备上进行测试。

I think the 3rd option would be the best since in BitSet even though its less memory consuming in some cases it can simply waste space if you store in binary mode,an example is: 我认为第三个选项是最好的,因为在BitSet中,即使在某些情况下它的内存消耗较少,如果以二进制模式存储,它只会浪费空间,例如:


BitSet b=new BitSet();
b.set(100000);

In this case you have only one set bit but it will simply waste space for all the unset bit's when you store in file either as bitmap or in serialized format.Anroid's parcel system I'm not aware about how it works so i can't comment on it. 在这种情况下,您只有一个设置位,但是当您以位图或序列化格式存储在文件中时,它只会浪费所有未设置位的空间。Anroid的包裹系统我不知道它是如何工作的,所以我无法对此发表评论。

If you are sure the bits won't be big numbers then go for the the binary mode else go for the XML mode or simply text where you store b.toString() and parse it from file when required. 如果您确定这些位不是大数字,则转到二进制模式,否则转到XML模式,或者仅将文本存储在存储b.toString()的地方,并在需要时从文件中进行解析。

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

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