timer.c

来自「tcpip协议第三卷源码,下载下来可直接编译」· C语言 代码 · 共 50 行

C
50
字号
/* * Copyright (c) 1996 W. Richard Stevens.  All rights reserved. * Permission to use or modify this software and its documentation only for * educational purposes and without fee is hereby granted, provided that * the above copyright notice appear in all copies.  The author makes no * representations about the suitability of this software for any purpose. * It is provided "as is" without express or implied warranty. */#include	"cliserv.h"#include	<sys/time.h>void	tvsub(struct timeval *, struct timeval *);struct timeval	tvstart, tvend;voidstart_timer(){	if (gettimeofday(&tvstart, NULL) < 0)		err_sys("start_timer: gettimeofday error");}doubleprint_timer(){	double	triptime;	if (gettimeofday(&tvend, NULL) < 0)		err_sys("print_timer: gettimeofday error");	tvsub(&tvend, &tvstart);	triptime = ((double) tvend.tv_sec) * 1000.0 +			   ((double) tvend.tv_usec) / 1000.0;		/* in millisec */	printf("rtt = %9.3f ms\n", triptime);	return(triptime);}/* Subtract two timeval structures: *end -= *start */voidtvsub(struct timeval *end, struct timeval *start){	if ((end->tv_usec -= start->tv_usec) < 0) {		--end->tv_sec;		end->tv_usec += 1000000;	}	end->tv_sec -= start->tv_sec;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?