void calc_sort_time(int *parray,int size,void (*sortfunc)(int *,int),char *funcname) { struct timeval start,stop,diff; memset(&start,0,sizeof(struct timeval)); memset(&stop,0,sizeof(struct timeval)); memset(&diff,0,sizeof(struct timeval)); gettimeofday(&start,NULL); sortfunc(parray,size); gettimeofday(&stop,NULL); time_substract(&diff,&start,&stop); printf("%s:%d s,%d us\n",funcname,(int)diff.tv_sec,(int)diff.tv_usec); }
double Etime(char** cmd) { Remove_first(cmd); // remove "etime" in cmd int i; char *ifIR=NULL, *ifOR=NULL; for(i=0; cmd[i]!=NULL;i++) { ifOR = strchr(cmd[i], '>'); if( ifOR!=NULL ) break; // if cmd contains > } for(i=0; cmd[i]!=NULL;i++) { ifIR = strchr(cmd[i], '<'); if( ifIR!=NULL ) break;// if cmd contains < } struct timeval start, finish, diff; gettimeofday(&start, 0); if(cmd[0] != NULL) { if( ifOR!=NULL ) output_red(cmd); else if( ifIR!=NULL ) input_red(cmd); else if((strcmp(cmd[0], "echo") == 0)||(strcmp(cmd[0], "exit") == 0)) check_command(cmd); else my_execute(cmd); } gettimeofday(&finish, 0); time_substract(&diff, &start, &finish); double duration = (double)diff.tv_sec + ((double)diff.tv_usec/1000000.0); printf("Elapsed Time: %.9fs\n",duration); return duration; }
int main(int argc, char **argv) { int i = 30; struct timeval start,stop,diff; memset(&start,0,sizeof(struct timeval)); memset(&stop,0,sizeof(struct timeval)); memset(&diff,0,sizeof(struct timeval)); while( i -- ) { gettimeofday(&start,0); //×ÄҪ×µÄÂ.. // printf("hello world\n"); usleep(50); gettimeofday(&stop,0); time_substract(&diff,&start,&stop); printf("usleep(50), Total time : %d s,%d us\n",(int)diff.tv_sec,(int)diff.tv_usec); } i = 30; while( i -- ) { gettimeofday(&start,0); //×ÄҪ×µÄÂ.. // printf("hello world\n"); usleep(5000); gettimeofday(&stop,0); time_substract(&diff,&start,&stop); printf("usleep(5000), Total time : %d s,%d us\n",(int)diff.tv_sec,(int)diff.tv_usec); } i = 30; while( i -- ) { gettimeofday(&start,0); //×ÄҪ×µÄÂ.. // printf("hello world\n"); nanosleep(50000); gettimeofday(&stop,0); time_substract(&diff,&start,&stop); printf("nanosleep(50000), Total time : %d s,%d us\n",(int)diff.tv_sec,(int)diff.tv_usec); } }