#include <sys/time.h> #include <stdio.h> #include <math.h> void function()/*用來耗用一定的時間而已,無實際用處的函數*/ { unsigned int i,j; double y; for(i=0;i<10000;i++) for(j=0;j<10000;j++) y=sin((double)i); } int main(int argc, char ** argv) { struct timeval tpstart,tpend; float timeuse; gettimeofday(&tpstart,NULL); function(); gettimeofday(&tpend,NULL); timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec; timeuse/=1000000; printf("Used Time:%fn",timeuse); exit(0); }
struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };