c - Are repeated recv() calls expensive? -


i have question situation face quite often. time time have implement various tcp-based protocols. of them define variable-length data packets begin common header ([packet id, length, payload] or similar). obviously, there can 2 approaches reading these packets:

  1. read header (since header length fixed), extract payload length, read payload
  2. read available data , store in buffer; parse buffer afterwards

obviously, first approach simple, requires 2 calls read() (or more). second 1 more complicated, requires less calls.

the question is: first approach affect performance badly enough worry it?

the best way answer measure. strace program decent purpose of measuring system call times. using adds lot of overhead in itself, if merely compare cost of 1 recv purpose versus cost of two, should reasonably meaningful. use -tt option times. or can use -c option overview of time spent separated syscall spent on.

a better way measure, albeit more of learning curve, oprofile.

also note if decide buffering worthwhile, may able use fdopen , stdio functions take care of you. extremely easy , work if you're dealing single connection or if have thread/process per connection, won't work @ if want use select/poll-based model.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -