码迷,mamicode.com
首页 > 其他好文 > 详细

测时间,精确到usec

时间:2014-08-19 12:26:34      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   ar   art   cti   

#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();        
}

不评论,就简单明了。

测时间,精确到usec,布布扣,bubuko.com

测时间,精确到usec

标签:style   blog   color   io   for   ar   art   cti   

原文地址:http://www.cnblogs.com/dpblue/p/3921614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!