/************************************************************************ * * * The ElapsedTime function returns the elapsed time since the * * last call to StartCronograph. * * * ************************************************************************/ #ifndef CLOCK_C #define CLOCK_C #undef CLOCK_TIME_VAL #undef CLOCK_DEBUG #include #include void StartCronograph(void); float ElapsedTime(void); #ifdef CLOCK_TIME_VAL struct timeval clock_tpi; #else clock_t clock_ti; #endif /* implementation */ void StartCronograph() { #ifdef CLOCK_TIME_VAL gettimeofday(&clock_tpi,NULL); #ifdef CLOCK_DEBUG printf("%ld.%06ld sec.\n", clock_tpi.tv_sec, clock_tpi.tv_usec ); #endif #else clock_ti=clock(); #ifdef CLOCK_DEBUG printf("%ld clocks\n", clock_ti); #endif #endif } float ElapsedTime() { float Elapsed; int Hou, Min, Sec; #ifdef CLOCK_TIME_VAL struct timeval clock_tpf; long ElapsedSec, ElapseduSec; gettimeofday(&clock_tpf,NULL); ElapsedSec=clock_tpf.tv_sec-clock_tpi.tv_sec; ElapseduSec=(clock_tpf.tv_usec-clock_tpi.tv_usec); if (ElapseduSec<0) { ElapsedSec--; ElapseduSec+=1000000; } #ifdef CLOCK_DEBUG printf("%ld.%06ld sec.\n", clock_tpf.tv_sec, clock_tpf.tv_usec); #endif Elapsed=(float)ElapsedSec+(float)ElapseduSec/1000000; #else clock_t clock_tf; clock_tf = clock(); #ifdef CLOCK_DEBUG printf("%ld clocks\n", clock_tf); #endif Elapsed=(double)(clock_tf-clock_ti)/CLOCKS_PER_SEC; #endif Hou=(int)(Elapsed/3600); Min=(int)((Elapsed-Hou*3600)/60); Sec=(int)(Elapsed-Hou*3600-Min*60); printf("\nElapsed time\t%.2f s\t(%dh %dm %ds)\n",Elapsed,Hou,Min,Sec); return(Elapsed); } /************************************************************************ #include int main() { int replic = 500; int final = 100000; int i,j; float elap; StartCronograph(); for (i=0; i