If all your data is really going to be ASCII, then you may be able to do it slightly faster than ASCIIEncoding
, which has various (entirely reasonable) bits of error handling etc. You may also be able to speed it up by avoiding creating new byte arrays all the time. Assuming you have an upper bound which all your messages will be under:
void QuickAndDirtyAsciiEncode(string chars, byte[] buffer)
{
int length = chars.Length;
for (int i = 0; i < length; i++)
{
buffer[i] = (byte) (chars[i] & 0x7f);
}
}
You'd then do something like:
readonly byte[] Buffer = new byte[8192]; // Reuse this repeatedly
...
QuickAndDirtyAsciiEncode(text, Buffer);
// We know ASCII takes one byte per character
socket.Send(Buffer, text.Length, SocketFlags.None);
This is pretty desperate optimisation though. I'd stick with ASCIIEncoding
until I'd proven that this was the bottleneck (or at least that this sort of grotty hack doesn't help).