-1

Background: I'm currently creating an application in which two Java programs communicate over a network using a DataInputStream and DataOutputStream. Before every communication, I'd like to send an indication of what type of data is being sent, so the program knows how to handle it. I was thinking of sending an integer for this, but a byte has enough possible combinations.

So my question is, is Java's DataInputStream's readByte() faster than readInt()?

Also, on the other side, is Java's DataOutputStream's writeByte() faster than writeInt()?

178865
  • 13
  • 4
  • 1
    `readByte()` reads one byte, `readInt()` reads four bytes, so ... – Arnaud Apr 17 '18 at 09:04
  • The difference of 1:4 bytes doesn't matter in the general overhead of getting bytes from here to there. Don't worry about picoseconds. – laune Apr 17 '18 at 09:06
  • Don't worry about performance. Worry about getting your program to work correctly. – Kayaman Apr 17 '18 at 09:07
  • If it is Java programs communicating: have you thought about using ObjectIn/OutputStreams? – laune Apr 17 '18 at 09:09
  • @laune that's not a good idea. Object streams for standard communication seem like an easy solution at first, but there are all sorts of quirks that you need to know, and it tends to get a bit limiting. – Kayaman Apr 17 '18 at 09:11
  • @Kayaman Perhaps - one has to look at other parameters. But using a low-level binary protocol is not the best choice either. – laune Apr 18 '18 at 01:17
  • @laune but Object streams **are** a low-level binary protocol. With downsides. – Kayaman Apr 18 '18 at 05:51
  • @Kayaman Just read the javadoc: one is clearly an extension of the other, one being restricted to primitives, the other works at object level. And there's some validation tests being done. Both programs are from the same source, which is essential - otherwise don't even think about object streams. – laune Apr 18 '18 at 11:42
  • @laune are you teaching me Java, or don't you just understand what a "binary protocol" means? Now object streams are definitely a binary protocol, with downsides. I suggested not to use them, and you also mentioned one of the downsides: binary compatibility must be maintained. – Kayaman Apr 18 '18 at 11:45
  • @Kayman I'm past teaching any programming languages; I've done so with 10+ languages at all levels. This is not a language question - my statement is just this: that a protocol that uses calls on an aggregate like an object or a data structure is less decidedly lower-level that writing individual calls for the smallest data units of the language. - In terms of XML, it would be the difference between print statements emitting 123 and similar compared against using (e.g.) JAXB marshal on an object. – laune Apr 19 '18 at 10:01

1 Answers1

0

If one byte will be enough for your data then readByte and writeByte will be indeed faster (because it reads/writes less data). It won't be noticeable difference though because the size of the data is very small in both cases - 1 vs 4 bytes.

If you have lots of data coming from the stream then using readByte or readInt will not make speed difference - for example calling readByte 4 times instead of readInt 1 time. Just use the one depending on what kind of data you expect and what makes your code easier to understand. You will have to read the whole stuff anyway :)

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23