I wrote a very simple program that one PC doing udp sendto, and another PC doing recvfrom. On 1Gbps ethernet link, the sender side cost 13% CPU but the receiver side cost only 5%.
Anyone know why sendto cost so much higher than recvfrom,and any good idea to reduce the sender cost, thanks very much!
main code :
while (1)
{
static int sendLen = 0;
sendLen = sendto(socketfd, buffer, buflen, 0, (struct sockaddr *)&dest, sizeof(dest));
totalSize += sendLen;
++loopcnt;
totalsend++;
if (loopcnt == COUNTNUM)
{
clock_gettime(0, &end);
unsigned int timecost = 1000 * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000; //ms
double timecost_double = 1000 * (end.tv_sec - start.tv_sec) + ((double)(end.tv_nsec - start.tv_nsec)) / 1000000;
printf("UDP APP TX %.1f M bps. PPS %.1f pps packet size:%d, timecost=%.lf\n", (float)(totalSize * 8) / (timecost_double * 1000),
(float)(loopcnt*1000) / (timecost), buflen, timecost_double); //timecost ms
start = end;
totalSize = 0;
loopcnt = 0;
}
}