测时间,精确到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();
}
不评论,就简单明了。
/***********************************************
看书看原版,原汁原味。
不会英文?没关系,硬着头皮看下去慢慢熟练,才会有真正收获。
没有原书,也要网上找PDF来看。
网上的原版资料多了去了,下载东西也到原始下载点去看看。
你会知其所以然,呵呵。
***********************************************/