Kevin Wang
Kevin Wang

Reputation: 3330

C# string to byte array speed

So coming off of this question:

Which is fast comparison: Convert.ToInt32(stringValue)==intValue or stringValue==intValue.ToString()

I am looking a base type for my networked application to be stored in packets.

The Idea:

  1. Packet class stores a list of (type)
  2. Add objects to the packet class
  3. Serialize and send it between machines
  4. Deserialize into (type)
  5. Convert (type) into the type of object you added originally.

Originally, I was using strings as (type). However, I am a bit dubious as every time I want to convert an int to a string, it seems like a tasking process. When I am communicating packets containing lots of uints to strings at 30FPS, I would like to make this process as fast as possible.

Therefore, I was wondering if byte[] would be a more suitable type. How fast is converting back and forth between a byte[] and ints/strings vs just strings to ints? BTW, I will not be sending a lot of strings on the network very often. Almost all of what I will be sending will be uints.

Upvotes: 1

Views: 1296

Answers (3)

FastAl
FastAl

Reputation: 6280

If you are using the same program on both ends, use BinarySerialization if possible. You are worried about speed; but unless this is just going between two processes on localhost, actual wire time, let alone latancy, will be orders of magnitude slower than any real conversion process.

Of course, don't concatenate strings; you will make a liar out of me.

The thing you need to save here is your coding time, plus the possibility of errors for rolling your own serialization. If you properly encapsulate the data transfer parts of your program, upgrading them would be easy. Trying to spend extra time making something fast is called premature optimization (google it - it's a valid argument - most of the time). If it is a bottleneck, leverage your encapsulated design, and change it. You won't spend that much extra time then if you'd done it first - but likely won't end up spending that time at all.

A warning about binary serialization. The types you are sending must be the same version and type name. If you can put the same version into production on both ends, easily, it's no worry. If you need more than this, or binaryserialization is too slow, look into FastJson, which makes big promises and is free, or something similar.

Upvotes: 1

Convert.ToInt32 is decently fast provided it does not fail. If it fails then you incur the overhead of a thrown/caught exception which is massive.

The byte [] vs. some other type dichotomy is false. The network transports all information as -- essentially -- an array of bytes. So whether a StreamReader wrapped around a NetworkStream is turning the byte [] into a String, or you are yourself, it's still getting done.

Upvotes: 1

Eugen Rieck
Eugen Rieck

Reputation: 65274

byte[] is the "natural" data type for socket operations, so this seems a good fit, ints/uints will be very fast to convert also. Strings are a bit different, but if you chose the natural encoding of the platform, this will be fast also.

Upvotes: 1

Related Questions