简体   繁体   English

套接字recv()一次一个字节

[英]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)? 用套接字一次(在性能方面recv()一次调用recv()是个好主意吗? Does it cause a context switch? 它会导致上下文切换吗? If so why is there a context switch? 如果是这样,为什么要进行上下文切换?

Calling recv() one byte at a time will negatively impact performance. 一次调用一个字节的recv()将对性能产生负面影响。 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. 每次调用都有一定的开销-过渡到内核模式,文件描述符查找,分派到协议专用的驱动程序,缓冲区/队列锁定等。使用较大的缓冲区调用recv()可以大大降低每次调用的平均开销字节。

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. RAM中只有一个字节,您不想为每个字节完成全部工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM