| Navigation:First_Community->Software->Linux development | Goto:New Topic•Setting•Search | |
CopyRight owned by the original author.--(www.MegaEntry.com)
1.时间的表示 2.时间的测量 3.计时器的使用 --------------------------------------------------------------------------------MegaEntry - Social networking and discussion site!
1。时间表示 在程序当中,我们经常要输出系统当前的时间,比如我们使用date命令的输出结果.这个时候我们可以使用下面两个函数 #includeCopyRight owned by the original author.--(www.MegaEntry.com)
time函数返回从1970年1月1日0点以来的秒数.存储在time_t结构之中.不过这个函数的返回值对于我们来说没有什么实际意义.这个时候我们使用第二个函数将秒数转化为字符串. 这个函数的返回类型是固定的:一个可能值为. Thu Dec 7 14:58:59 2000 这个字符串的长度是固定的为26 2。时间的测量 有时候我们要计算程序执行的时间.比如我们要对算法进行时间分析.这个时候可以使用下面这个函数. #includeCopyRight owned by the original author.--(www.MegaEntry.com)
int gettimeofday(struct timeval *tv,struct timezone *tz); strut timeval { long tv_sec; /* 秒数 */ long tv_usec; /* 微秒数 */ };CopyRight owned by the original author.--(www.MegaEntry.com)
gettimeofday将时间保存在结构tv之中.tz一般我们使用NULL来代替. #includeCopyRight owned by the original author.--(www.MegaEntry.com)
y=sin((double)i); } main() { struct timeval tpstart,tpend;CopyRight owned by the original author.--(www.MegaEntry.com)
float timeuse; gettimeofday(&tpstart,NULL); function(); gettimeofday(&tpend,NULL); timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+CopyRight owned by the original author.--(www.MegaEntry.com)
tpend.tv_usec-tpstart.tv_usec; timeuse/=1000000; printf( "Used Time:%f ",timeuse); exit(0); }CopyRight owned by the original author.--(www.MegaEntry.com)
这个程序输出函数的执行时间,我们可以使用这个来进行系统性能的测试,或者是函数算法的效率分析.在我机器上的一个输出结果是: Used Time:0.556070 3。计时器的使用 Linux操作系统为每一个进程提供了3个内部间隔计时器. ITIMER_REAL:减少实际时间.到时的时候发出SIGALRM信号. ITIMER_VIRTUAL:减少有效时间(进程执行的时间).产生SIGVTALRM信号. ITIMER_PROF:减少进程的有效时间和系统时间(为进程调度用的时间).这个经常和上面一个使用用来计算系统内核时间和用户时间.产生SIGPROF信号. 具体的操作函数是:MegaEntry - Social networking and discussion site!
#includeCopyRight owned by the original author.--(www.MegaEntry.com)
struct itimerval { struct timeval it_interval; struct timeval it_value; } getitimer函数得到间隔计时器的时间值.保存在value中 setitimer函数设置间隔计时器的时间值为newval.并将旧值保存在oldval中. which表示使用三个计时器中的哪一个. itimerval结构中的it_value是减少的时间,当这个值为0的时候就发出相应的信号了. 然后设置为it_interval值.CopyRight owned by the original author.--(www.MegaEntry.com)
#includeMegaEntry - Social networking and discussion site!
#define PROMPT "时间已经过去了两秒钟 a " char *prompt=PROMPT; unsigned int len;CopyRight owned by the original author.--(www.MegaEntry.com)
void prompt_info(int signo) { write(STDERR_FILENO,prompt,len); } void init_sigaction(void)MegaEntry - Social networking and discussion site!
{ struct sigaction act; act.sa_handler=prompt_info; act.sa_flags=0; sigemptyset(&act.sa_mask); sigaction(SIGPROF,&act,NULL);MegaEntry - Social networking and discussion site!
} void init_time() { struct itimerval value; value.it_value.tv_sec=2;CopyRight owned by the original author.--(www.MegaEntry.com)
value.it_value.tv_usec=0; value.it_interval=value.it_value; setitimer(ITIMER_PROF,&value,NULL); } int main()MegaEntry - Social networking and discussion site!
{ len=strlen(prompt); init_sigaction(); init_time(); while(1); exit(0);MegaEntry - Social networking and discussion site!
} 这个程序每执行两秒中之后会输出一个提示.CopyRight owned by the original author.--(www.MegaEntry.com)