在Java中使用对象数据流和原始数据流哪个更有效率?

发布于 2024-10-12 05:05:08 字数 186 浏览 3 评论 0原文

在 Java 中执行 I/O 操作时,使用像 ObjectInputStream 这样的对象数据流通常更有效(就计算资源而言),还是使用像 这样的原始数据流更有效>数据输入流

如果使用对象数据流更有效,那么当我们考虑到与创建对象相关的开销(尽可能小)时,对象数据流是否仍然更有效?

When performing I/O operations in Java, is it generally more efficient (in terms of computing resources) to use an object data stream like ObjectInputStream or is it more efficient to use a primitive data stream like DataInputStream?

If it is more efficient to use an object data stream, is an object data stream still more efficient when we take into account the overhead (as minimal as it may be) associated with creating the object?

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

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

发布评论

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

评论(5

后知后觉 2024-10-19 05:05:08

如果做得正确的话,自定义序列化可以节省空间/带宽和 CPU 时间。

我建议您看一下 google 的 Protocol Buffers - 非常高效且可移植的序列化库。

Custom serialization can save you space/bandwidth and cpu time, if done right.

I suggest you take a look at google's Protocol Buffers - very efficient and portable serialization library.

一身仙ぐ女味 2024-10-19 05:05:08

您可以根据流中实际的数据类型选择对象或数据输入流。如果它是 Java 对象,则使用对象流,如果不是,则使用 DataInputStream(或其他适当的流)。

那么您的流中有哪些类型的数据?

You choose an Object or Data InputStream based on what kind of data is actually in your stream. If it's Java Objects use an Object stream, if it's not then a DataInputStream (or other as appropriate).

So what kind of data do you have in your stream?

二手情话 2024-10-19 05:05:08

ObjectInputStream 使用起来会更简单。在尝试使用 DataInputStream 之前,我会检查一下这是否足够快。 DataInputStream 的效率不如使用 NIO/ByteBuffer,但相对较小的改进却增加了工作量。

ObjectInputStream will be simpler to use. I would check this won't be fast enough before going to the effort of using DataInputStream. DataInputStream is not as efficient as using NIO/ByteBuffer but the effort increases for a relatively small improvement.

岛歌少女 2024-10-19 05:05:08

对象序列化/反序列化是java中较重的事情之一。因此,如果这是程序的性能关键部分,那么避免对象序列化会有所帮助。尽管在许多情况下,瓶颈位于其他地方,并且您最终可能会进行过早的优化。因此,恕我直言,如果您不确定这将成为瓶颈,请使用 ObjectInputStream 并稍后进行优化。

Object serialization/desirialization is one of the heavier things there are in java. So if this is a performance critical part of your program then avoiding object serialization can help. Although in many cases the bottlenecks lie somewhere else and you might end up doing premature optimization. So IMHO if you don't know for sure that this is going to be a bottleneck use ObjectInputStream and optimize later.

你好,陌生人 2024-10-19 05:05:08

为什么你不写一些可以实现你想要的功能的东西,对其进行一些测试,这样你就知道它是有效的。然后,如果它成为问题,您将能够用更快的东西替换实现,而无需大惊小怪和麻烦。如果事实证明这不是问题,就像大多数这样的事情一样……那么您就不会花时间在不必要的事情上,也不会过早地进行优化。

Xstream是相当好用的火这种东西,而且非常好用。

如果您的架构不是过于繁琐,您可能永远不需要优化这一点。

Why Don't you write something that does what you want, put some tests around it so you know it works. Then if it ever becomes a problem, you will be able to replace the implementation with something quicker, with no fuss and bother. If it turns out that it's not an issue, as most things like this turn out to be...then you won't have spent time on something unnecessary, and you won't have prematurely optimised.

XstreamIs pretty handy fire this kind of thing, and very easy to use.

if your architecture isn't unduely chatty you may never need to optimise this bit.

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