任何人都可以解释为什么我总是会从下面的代码中得到0的时间?我只是想要一个毫秒计时器来计算从套接字发送和接收数据之间的延迟,但不管我尝试什么,我总是得到0的结果......我甚至试过微秒,以防万一我的系统在低于1毫秒。C Unix毫秒计时器返回差值0
printf("#: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
struct timeval start, end;
unsigned long mtime, seconds, useconds;
gettimeofday(&start, NULL);
n = write(clientSocket,buffer,strlen(buffer));
if (n < 0)
{
error("Error: Unable to write to socket!\n");
}
bzero(buffer,256);
n = read(clientSocket,buffer,255);
gettimeofday(&end, NULL);
seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;
mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
if (n < 0)
{
error("Error: Unable to read from socket!\n");
}
printf("%s\n",buffer);
printf("Delay: %lu microseconds\n", useconds);
因为阅读时间不到1毫秒,也许? – 2013-08-26 04:44:19
“我甚至试过微秒,以防万一我的系统在不到1ms内执行它。”还有其他建议吗? – T3CHN0CR4T
你是怎么试微秒的? (你知道这些函数实际上没有微秒的分辨率吗?) – 2013-08-26 04:48:17