在 Android 中使用 Serialized 不好吗?

发布于 2024-09-16 22:11:54 字数 350 浏览 12 评论 0原文

我读过很多帖子和文章,称赞 Parcelable 的速度比 Serialized 快。我已经使用两者通过意图在活动之间传递数据有一段时间了,但在两者之间切换时尚未注意到任何速度差异。我必须传输的典型数据量是 5 到 15 个嵌套对象,每个对象有 2 到 5 个字段。

由于我有大约 30 个必须可转移的类,因此实现 Parcelable 需要大量样板代码,这会增加维护时间。我目前的要求之一也是编译后的代码应该尽可能小;我希望通过使用 Serialized 而不是 Parcelable 可以节省一些空间。

对于如此少量的数据,我应该使用 Parcelable 还是没有理由使用它而不是 Serialized?或者还有其他原因为什么我不应该使用可序列化?

I've been reading a lot of posts and articles extolling the speed of Parcelable over Serializable. I've been using both for a while to pass data between Activities through Intents, and have yet to notice any speed difference when switching between the two. The typical amount of data I have to transfer is 5 to 15 nested objects with 2 to 5 fields each.

Since I have about 30 classes which must be transferable, implementing Parcelable requires a lot of boilerplate code that adds maintenance time. One of my current requirements is also that the compiled code should be as small as possible; I expect that I could spare some space by using Serializable over Parcelable.

Should I use Parcelable or is there no reason to use it over Serializable for such small amounts of data? Or is there another reason why I shouldn't use Serializable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

国产ˉ祖宗 2024-09-23 22:11:54

对于内存中的使用,Parcelable 远远优于 Serialized。我强烈建议不要使用可序列化。

您不能将 Parcelable 用于将存储在磁盘上的数据(因为当情况发生变化时,它不能很好地保证数据一致性),但是 Serialized 太慢,我强烈建议不要在那里使用它。您最好自己编写数据。

此外,Serialized 的性能问题之一是它最终会旋转大量临时对象,从而导致应用程序中出现大量 GC 活动。这是非常令人发指的。 :}

For in-memory use, Parcelable is far, far better than Serializable. I strongly recommend not using Serializable.

You can't use Parcelable for data that will be stored on disk (because it doesn't have good guarantees about data consistency when things change), however Serializable is slow enough that I would strongly urge not using it there either. You are better off writing the data yourself.

Also, one of the performance issues with Serializable is that it ends to spin through lots of temporary objects, causing lots of GC activity in your app. It's pretty heinous. :}

古镇旧梦 2024-09-23 22:11:54

继续使用序列化。您会在网上看到很多人告诉您序列化非常慢且效率低下。这是正确的。但是,作为一名计算机程序员,你永远不想做的一件事就是将任何有关性能的评论视为绝对。

问问自己序列化是否会减慢程序的速度。您是否注意到它何时从一个活动转到另一个活动?您注意到它何时保存/加载吗?如果没有,也没关系。当您使用大量手动序列化代码时,您不会获得更小的占用空间,因此没有任何优势。那么,如果它比替代方案慢 100 倍(如果慢 100 倍意味着 10 毫秒而不是 0.1 毫秒)怎么办?你也不会看到,所以谁在乎呢?而且,为什么有人会投入大量精力为 30 个类编写手动序列化,而这不会在性能上产生任何明显的差异呢?

Continue to use Serialization. You'll see lots of people online who will tell you that Serialization is very slow and inefficient. That is correct. But, one thing you never want to do as a computer programmer is take any comment about performance as an absolute.

Ask yourself if serialization is slowing your program down. Do you notice when it goes from activity to activity? Do you notice when it saves/loads? If not, it's fine. You won't get a smaller footprint when you go to a lot of manual serialization code, so there is no advantage there. So what if it is 100 times slower than an alternative if 100 times slower means 10ms instead of 0.1ms? You're not going to see either, so who cares? And, why would anyone invest massive effort into writing manual serialization for 30 classes when it won't make any perceptible difference in performance?

離人涙 2024-09-23 22:11:54

每个人都只是盲目地声称 Parcelable 比 Serialized 更好、更快,但没有人试图用任何证据来支持他的说法。我决定亲自测试一下,得到了非常有趣的结果。

一般 Android 设备上的普通 Java 序列化(如果做得正确)的写入速度比 Parcelable 快约 3.6 倍,读取速度约快 1.6 倍。

您可以在这里查看我的测试项目:https://github.com/afrish/androidserializationtest

Everybody just blindly states that Parcelable is better and faster, than Serializable, but nobody has tried to support his statements with any proofs. I decided to test this myself and I got very interesting results.

Usual Java serialization on an average Android device (if done right) is about 3.6 times faster than Parcelable for writes and about 1.6 times faster for reads.

You can check my test project here: https://github.com/afrish/androidserializationtest

情深已缘浅 2024-09-23 22:11:54

有没有人考虑过使用 JSON 进行序列化并将数据作为字符串传递? GSON 和 Jackson 应该足够高效,可以成为 Parcelable 和 Serialized 的竞争对手。

Has anyone considered serialization using JSON and passing the data as a string? GSON and Jackson should be efficient enough to be a competitor to Parcelable as well as Serializable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文