测时间,精确到usec

#include <sys/time.h> 
#include <stdio.h> 
#include <math.h>

/* 
    accurate to usec
*/
void getusertime()
{
    static struct timeval tpstart,tpend; 
    static float timeuse; 
    static int count=0;
    count++;
    if ((count%2)==1)
            gettimeofday(&tpstart,NULL);
    else
    {
        gettimeofday(&tpend,NULL);          
        timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+ \
            tpend.tv_usec-tpstart.tv_usec; 
        timeuse/=1000000;   
        printf("count:%d  Used Time:%f\n",count,timeuse); 
    }
}

void testfunction() 
{ 
unsigned int i,j; 
double y; 
for(i=0;i<1000;i++) 
for(j=0;j<1000;j++) 
y=sin((double)i); 
} 

void test()
{
    getusertime();      
    testfunction();     
    getusertime();

}

int main(int argc,char** argv) 
{    
    for(int i=0;i<10;i++)
        test();        
}

不评论,就简单明了。

posted on 2014-08-19 11:25  深蓝无忌  阅读(404)  评论(0编辑  收藏  举报

导航