关于select定时器和usleep定时器的准确率说明

来源:互联网 发布:淘宝上买了把刀被拘留 编辑:程序博客网 时间:2024/06/05 02:33

   。。。。。。
    unsigned int nSec= 0;
    unsigned int nUSec= 0;
    struct timeval tvBegin, tvNow;
    int delay[20] = {500000,100000,50000,10000,1000,900,500,100,10,1,0};
    int nReaduce = 0;
    int nDelay = 0;
    int iTimeTest = 0;


    for( int i = 0; i<11; i++)
    {
        nDelay = delay[i];
        bzero(&tvBegin, sizeof(tvBegin));
        bzero(&tvNow, sizeof(tvNow));


        gettimeofday(&tvBegin, NULL);

usleep(nDelay);
        //fntimer(0,nDelay);
        gettimeofday(&tvNow, NULL);


        iTimeTest =( (tvNow.tv_sec-tvBegin.tv_sec)*1000000+tvNow.tv_usec-tvBegin.tv_usec);
        printf("%d\n", iTimeTest - nDelay);
    }
    return E_OK;
}
      

int fntimer(int sec, int msec)
{
    struct timeval temp;
    temp.tv_sec = sec;
    temp.tv_usec = msec;


    select(0,NULL ,NULL, NULL, &temp);
    return E_OK;
}


在使用usleep时结果为打印的结果为:

1108
832 
974 
937 
907 
1077
475 
907 
979 
986 
1005

在使用fntimer()时结果为打印的结果为:

-438
-35 
-17 
-9  
-35 
109 
488 
889 
978 
987 
3   

所以认为fntimer()准确性优于usleep。

0 0
原创粉丝点击