Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Object Stream efficiency over network

Quick design question: I need to implement a form of communication between a client-server network in my game-engine architecture in order to send events between one another.

I had opted to create event objects and as such, I was wondering how efficient it would be to serialize these objects and pass them through an object stream over the simple socket network?

That is, how efficient is it comparatively to creating a string representation of the object, sending the string over via a char stream, and parsing the string client side?

The events will be sent every game loop, if not more; but the event object itself is just a simple wrapper for a few java primitives.

Thanks for your insight!

(tl;dr - are object streams over networks efficient?)

like image 488
WATWF Avatar asked Nov 03 '11 21:11

WATWF


People also ask

Are Java streams more efficient?

Twice as better than a traditional for loop with an index int. Among the Java 8 methods, using parallel streams proved to be more effective.

Are Java streams faster than for loops?

Yes, streams are sometimes slower than loops, but they can also be equally fast; it depends on the circumstances. The point to take home is that sequential streams are no faster than loops.

Why is Java 8 streaming so fast?

In Java8 Streams, performance is achieved by parallelism, laziness, and using short-circuit operations, but there is a downside as well, and we need to be very cautious while choosing Streams, as it may degrade the performance of your application. Let us look at these factors which are meant for Streams' performance.

What are the advantages of Java streams?

There are a lot of benefits to using streams in Java, such as the ability to write functions at a more abstract level which can reduce code bugs, compact functions into fewer and more readable lines of code, and the ease they offer for parallelization.


1 Answers

If performance is the primary issue, I suggest using Protocol Buffers over both your own custom serialization and Java's native serialization.

Jon Skeet gives a good explanation as well as benchmarks here: High performance serialization: Java vs Google Protocol Buffers vs ...?

If you can't use PBs, I suspect Java's native serialization will be more optimized than manually serializing/deserializing from a String. Whether or not this difference is significant is likely dependent on how complex of an object you're serializing. As always, you should benchmark to confirm your predictions.

The fact that you're sending things over a network shouldn't matter.

like image 59
tskuzzy Avatar answered Sep 17 '22 22:09

tskuzzy