c++ - What could be the format specifier for the below program? -
void task(task100ms_raster) { int a,b,c; struct timespec start, stop; uint32 starttime, stoptime; uint32 duration100ms; if( clock_gettime( clock_realtime, &start) == -1 ) { perror( "clock gettime" ); } starttime =start.tv_sec + 0.0000001 * start.tv_nsec; printf("start time %lu", starttime); printf("value %d",c); printf("etas1\n"); if( (stoptime = clock_gettime( clock_realtime, &stop)) == -1 ) { perror( "clock gettime" ); } stoptime = stop.tv_sec + 0.0000001 * stop.tv_nsec; printf("stop time %lu", stoptime); duration100ms = stoptime -starttime; printf( "time difference is= %lu\n", duration100ms ); }
i created timer calculating start time, stop time , difference between time. confused format specifier start time, stop time , suration100ms. struct timespec start, stop; of type http://www.qnx.com/developers/docs/6.3.0sp3/neutrino/lib_ref/t/timespec.html correct format specifiers results. guide me.
you're doing floating-point calculation compute number of seconds, storing result in integer:
starttime = start.tv_sec + 0.0000001 * start.tv_nsec;
this truncate result whole integers. should make starttime
double
, , print such:
double starttime = start.tv_sec + 1e-9 * start.tv_nsec; printf("start time %f\n", starttime);
i think it's better precision, convert float when compute difference:
double duration = stoptime.tv_sec - starttime.tv_sec + 1e-9 * (stoptime.tv_nsec - starttime.tv_nsec);
if must have duration integer number in units of 100 ms, do:
duration = 10000 * (stoptime.tv_sec - starttime.tv_sec) + (stoptime.tv_nsec - starttime.tv_nsec) / 100000000;
Comments
Post a Comment