代码如下:
#include <stdio.h>
#include <sys/time.h>
void function()
{
unsigned int i,j;
double y;
for (i=0;i<1000;i++)
for (j=0;j<1000;j++)
y=i/(j+1);
}
int main()
{
struct timeval tpatart,tpend;
float timeuse;
gettimeofday(&tpatart,NULL);
function();
gettimeofday(&tpend,NULL);
timeuse=1000000*(tpend.tv_sec-tpatart.tv_sec)+tpend.tv_usec-tpatart.tv_usec;
printf ("time spend are %f\n",timeuse/1000000);
return 0;
}
caution the line
y=i/(j+1);
you must use the “()” or you will see the float expection
then you will see those string in the shell like this~
[root@iwly]# ./precisiontime
time spend are 0.027556