Curious
Curious

Reputation: 21560

Socket recv() one byte at a time

Is it a good idea to call recv() one byte at a time with sockets (in terms of performance)? Does it cause a context switch? If so why is there a context switch?

Upvotes: 1

Views: 3413

Answers (2)

deviantfan
deviantfan

Reputation: 11434

No, it's absolutely not a good idea.

Calling the read function has a lot of overhead compared to just reading and writing
one byte in RAM, you don't want to do the whole work for every byte.

Upvotes: 0

keithmo
keithmo

Reputation: 4943

Calling recv() one byte at a time will negatively impact performance. There is a certain amount of overhead on each call -- the transition to kernel mode, file descriptor lookup, dispatch to the protocol-specific driver, buffer/queue locking, etc. Calling recv() with larger buffers greatly decreases the average overhead per byte.

Upvotes: 2

Related Questions